使用托管在 Heroku 上的 Selenium 登录 Instagram 时无法找到元素 'username'

Unable to locate element 'username' while logging in Instagram with Selenium hosted on Heroku

我在使用 Instagram 登录时遇到 Selenium 的无头模式问题。我找到了 Selenium 无头模式的默认 Web 代码片段,但它无法在网页上找到特定元素(例如 Instagram 主页中的用户名)。该代码在我的电脑上本地运行良好,但是当它部署在 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 you can use either of the following 上的 用户名 字段中发送字符序列:

  • 使用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")
    

但是,username 字段 Instagram is a ReactJS enabled element, so ideally, to send a character sequence to the element you need to induce WebDriverWait for the and you can use either of the following :

  • 使用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
    
  • 浏览器快照: