如何使用 Chromedriver 和 Selenium Python 在 Instagram

这是经过检查的源代码


input aria-label="Phone number, username, or email" aria-required="true" autocapitalize="off" autocorrect="off" maxlength="75" name="username" type="text" class="_2hvTZ pexuQ zyHYP" value=""

我试过这段代码运行


driver = webdriver.Chrome()

driver.get('https://www.instagram.com/')

driver.find_element_by_xpath("//input[@name=\"username\"]").send_keys(username)

driver.find_element_by_xpath("//input[@name=\"password\"]").send_keys(pw)

driver.find_element_by_xpath('//button[@type="submit"]').click()

但是有这样的错误


selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@name="username"]"}

  (Session info: chrome=83.0.4103.61)

我的 chromedriver 和 chrome 版本匹配,并按照说明查找元素。为什么会出现此错误?


Cats萌萌
浏览 109回答 4
4回答

波斯汪

Instagram应用程序是通过React元素构建的。因此,在您开始搜索登录元素时调用 url 之后,您将面临NoSuchElementException解决方案要使用一组有效的凭据在Instagram中登录,您需要引入WebDriverWait并且element_to_be_clickable()您可以使用以下 定位器策略:使用XPATH:driver.get("https://www.instagram.com/")WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("username")driver.find_element_by_xpath("//input[@name='password']").send_keys("password")driver.find_element_by_xpath("//button/div[text()='Log In']").click()注意:您必须添加以下导入:from selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as EC浏览器快照:

牧羊人nacy

观察打开 instagram 主页时,它会在登录表单上显示微调器片刻,然后显示字段。因此,您需要在脚本中管理同步。在您的代码中使用显式等待,直到所需字段准备好进行交互。username = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='username']")))username.send_keys('username')password = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@name='password']")))password.send_keys('pw')需要导入以下包from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC

茅侃侃

试试下面的代码:driver = webdriver.Chrome()driver.get('https://www.instagram.com/')txt_user = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'username')))txt_user.send_keys('yourUserName')txt_pwd = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, 'password')))txt_pwd.send_keys('yourPassword')btn_submit = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[type="submit"]')))btn_submit.click()导入后:from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC

四季花海

from selenium import webdriverfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = webdriver.Chrome()  driver.get('https://www.instagram.com/')element = WebDriverWait(driver, 2).until(    EC.presence_of_element_located((By.ID, "//input[@name=\"username\"]")))element.sendkeys('user')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python