如何使用Chromedriver和Selenium Python在Instagram登录页面中找到用户名和密码字段 [英] How to locate the username and password field within Instagram login page using Chromedriver and Selenium Python

查看:419
本文介绍了如何使用Chromedriver和Selenium Python在Instagram登录页面中找到用户名和密码字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是经过检查的源代码

Here's inspected source code

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版本是匹配的,并按照以下说明查找元素.为什么会出现此错误?

My chromedriver and chrome version are match, and finding elements by following instruction. Why am I getting this error?

推荐答案

Instagram 应用程序是通过反应元素.因此,在启动搜索登录元素时调用URL之后,您将面对

Instagram application is built through React elements. Hence just after invoking the url when you initiate the search for the login element, you face NoSuchElementException

要使用一组有效的凭据在 Instagram 中登录,您需要诱导定位器策略:

To login within Instagram using a valid set of credentials you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

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

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 浏览器快照:

    您可以在以下位置找到一些相关的讨论:

    You can find a couple of relevant discussions in:

    • Filling in login forms in Instagram using selenium and webdriver (chrome) python OSX
    • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium

    这篇关于如何使用Chromedriver和Selenium Python在Instagram登录页面中找到用户名和密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆