• <small id='1IijF'></small><noframes id='1IijF'>

      <legend id='1IijF'><style id='1IijF'><dir id='1IijF'><q id='1IijF'></q></dir></style></legend>

        <tfoot id='1IijF'></tfoot>
        • <bdo id='1IijF'></bdo><ul id='1IijF'></ul>
        <i id='1IijF'><tr id='1IijF'><dt id='1IijF'><q id='1IijF'><span id='1IijF'><b id='1IijF'><form id='1IijF'><ins id='1IijF'></ins><ul id='1IijF'></ul><sub id='1IijF'></sub></form><legend id='1IijF'></legend><bdo id='1IijF'><pre id='1IijF'><center id='1IijF'></center></pre></bdo></b><th id='1IijF'></th></span></q></dt></tr></i><div id='1IijF'><tfoot id='1IijF'></tfoot><dl id='1IijF'><fieldset id='1IijF'></fieldset></dl></div>

        使用Heroku上托管的Selenium登录Instagram时,找不到元素&#39;用户名&#39;

        时间:2024-04-20
          <bdo id='fId3N'></bdo><ul id='fId3N'></ul>

            <legend id='fId3N'><style id='fId3N'><dir id='fId3N'><q id='fId3N'></q></dir></style></legend>

            1. <small id='fId3N'></small><noframes id='fId3N'>

                • <tfoot id='fId3N'></tfoot>
                  <i id='fId3N'><tr id='fId3N'><dt id='fId3N'><q id='fId3N'><span id='fId3N'><b id='fId3N'><form id='fId3N'><ins id='fId3N'></ins><ul id='fId3N'></ul><sub id='fId3N'></sub></form><legend id='fId3N'></legend><bdo id='fId3N'><pre id='fId3N'><center id='fId3N'></center></pre></bdo></b><th id='fId3N'></th></span></q></dt></tr></i><div id='fId3N'><tfoot id='fId3N'></tfoot><dl id='fId3N'><fieldset id='fId3N'></fieldset></dl></div>

                    <tbody id='fId3N'></tbody>
                  本文介绍了使用Heroku上托管的Selenium登录Instagram时,找不到元素&#39;用户名&#39;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在使用Instagram登录时使用无头模式的硒有问题。我找到了Selenium无头模式的默认Web代码片段,但是在网页上找不到特定的元素(比如Instagram主页中的用户名)。代码在我的PC上本地运行良好,但当它部署在Heroku上时,它会显示错误。错误日志位于代码下方。

                  from selenium import webdriver
                  from selenium.webdriver.common.keys import Keys
                  import time
                  import random as rd
                  import os
                  import schedule
                  
                  
                  def job():
                    try:
                  
                      user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36"
                  
                      options = webdriver.ChromeOptions()
                      options.headless = True
                      options.add_argument(f'user-agent={user_agent}')
                      options.add_argument("--window-size=1920,1080")
                      options.add_argument('--ignore-certificate-errors')
                      options.add_argument('--allow-running-insecure-content')
                      options.add_argument("--disable-extensions")
                      options.add_argument("--proxy-server='direct://'")
                      options.add_argument("--proxy-bypass-list=*")
                      options.add_argument("--start-maximized")
                      options.add_argument('--disable-gpu')
                      options.add_argument('--disable-dev-shm-usage')
                      options.add_argument('--no-sandbox')
                      options.binary_location = os.environ.get("GOOGLE_CHROME_BINARY")
                  
                      CHROMEDRIVER_PATH = os.environ.get("CHROMEDRIVER_PATH")
                      wd = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,chrome_options=options)
                      
                      wd.get('https://instagram.com')
                      time.sleep(rd.uniform(9,11))
                  
                      username = os.environ.get("INSTAGRAM_USER")
                      password = os.environ.get("INSTAGRAM_PASSWORD")
                  
                      time.sleep(rd.uniform(2.5,3.5))
                      wd.find_element_by_name('username').send_keys(username)
                      time.sleep(rd.uniform(0.95,1.45))
                      wd.find_element_by_name('password').send_keys(password + Keys.ENTER)
                      time.sleep(rd.uniform(6,8))
                  
                      wd.get('https://instagram.com')
                      time.sleep(rd.uniform(2.5,3.5))
                      print("SUCCESS")
                  
                      wd.quit()
                    except Exception as e:
                      print(e)
                  
                  
                  schedule.every(3).minutes.do(job)
                  
                  
                  while True:
                    schedule.run_pending()
                    time.sleep(10)
                  

                  错误:

                  Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="username"]"} 
                  (Session info: headless chrome=88.0.4324.96)
                  

                  推荐答案

                  要在Instagram上的用户名字段中发送字符序列,您可以使用以下任一Locator Strategies:

                  • 使用css_selector

                    driver.find_element(By.CSS_SELECTOR, "input[name='username']").send_keys("Artem")
                    
                  • 使用xpath

                    driver.find_element(By.XPATH, "//input[@name='username']").send_keys("Artem")
                    

                  但是,Instagram上的username字段是启用了ReactJS的元素,因此理想情况下,要将字符序列发送到您需要为element_to_be_clickable()诱导WebDriverWait的元素,您可以使用以下Locator Strategies之一:

                  • 使用CSS_SELECTOR

                    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='username']"))).send_keys("Artem")
                    
                  • 使用XPATH

                    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("Artem")
                    
                  • 注意::您必须添加以下导入:

                    from selenium.webdriver.support.ui import WebDriverWait
                    from selenium.webdriver.common.by import By
                    from selenium.webdriver.support import expected_conditions as EC
                    
                  • 浏览器快照:

                  这篇关于使用Heroku上托管的Selenium登录Instagram时,找不到元素&#39;用户名&#39;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:让Tkinter等待直到按钮被按下 下一篇:带有实例方法的Python函数工具lru_cache:Release对象

                  相关文章

                    <legend id='1Wbme'><style id='1Wbme'><dir id='1Wbme'><q id='1Wbme'></q></dir></style></legend>
                    1. <small id='1Wbme'></small><noframes id='1Wbme'>

                        <bdo id='1Wbme'></bdo><ul id='1Wbme'></ul>
                    2. <tfoot id='1Wbme'></tfoot>

                      <i id='1Wbme'><tr id='1Wbme'><dt id='1Wbme'><q id='1Wbme'><span id='1Wbme'><b id='1Wbme'><form id='1Wbme'><ins id='1Wbme'></ins><ul id='1Wbme'></ul><sub id='1Wbme'></sub></form><legend id='1Wbme'></legend><bdo id='1Wbme'><pre id='1Wbme'><center id='1Wbme'></center></pre></bdo></b><th id='1Wbme'></th></span></q></dt></tr></i><div id='1Wbme'><tfoot id='1Wbme'></tfoot><dl id='1Wbme'><fieldset id='1Wbme'></fieldset></dl></div>