简体   繁体   English

无法通过 xpath 使用 Selenium 和 Python 定位元素

[英]Unable to locate element by xpath using Selenium with Python

I am creating a Selenium bot in Python.我正在用 Python 创建一个 Selenium 机器人。 The driver is supposed to open instagram.com and fill in the login form using the users credentials as an input.驱动程序应该打开 instagram.com 并使用用户凭据作为输入填写登录表单。 There is an error:有一个错误:

# FIRST I OPEN INSTAGRAM LIKE THIS
self.driver.get("https://www.instagram.com/")

# THEN A POP-UP APPEARS, AND I CLOSE IT BY CLICKING IN THE ACCEPT BUTTON,
# IT SEEMS LIKE IT IS NOT AN IFRAME SO NO FRAME SWITCHES ARE NEEDED
cooki = self.driver.find_element_by_xpath('/html/body/div[2]/div/div/div/div[2]/button[1]')
cooki.click() 
 
# NOW I TRY TO FIND THE USERNAME FORM TO WRITE IN AN INPUT
usbar = self.driver.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]')    # ERROR HERE
usbar.send_keys(input('Username: '))

The error says:错误说:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="loginForm"]/div/div[1]"} NoSuchElementException:没有这样的元素:无法定位元素:{"method":"xpath","selector":"///*[@id="loginForm"]/div/div[1]"}

It cannot find the Username form to write in it.它找不到要在其中写入的用户名表单。 I believe there are no iframes in the way, I don't know if there is any other issue preventing the driver to find it.我相信没有 iframes,我不知道是否有任何其他问题阻止驱动程序找到它。 I am fairly new so I can't upload an image of the html of the page yet, that's why I include the line that opens the page.我是新手,所以我还不能上传页面 html 的图像,这就是为什么我包含打开页面的行。 Can anyone come up with a foolproof solution?谁能想出一个万无一失的解决方案?

From what I can see there are two problems.据我所知,有两个问题。

You are trying to call send_keys() on a div element.您正在尝试在div元素上调用send_keys() What you are likely looking for is the input element instead, which is nested inside of that div element.您可能正在寻找的是input元素,它嵌套在该div元素内。

This can be obtained by changing your XPATH slightly:这可以通过稍微更改 XPATH 来获得:

element = self.driver.find_element_by_xpath('//*[@name="username"]')
element.send_keys('...')

Another possible issue is that elements aren't always loaded immediately - you sometimes have to wait for the element to appear in the HTML before finding it.另一个可能的问题是元素并不总是立即加载 - 有时您必须等待元素出现在 HTML 中才能找到它。 This can be achieved using Webdriver.Wait()这可以使用Webdriver.Wait()来实现

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

# Wait for the element to appear in the HTML for a maximum of 10 seconds
element = WebDriverWait(self.driver, 10).until(
    EC.presence_of_element_located((By.NAME, "username"))
)

# Send input to the element
element.send_keys('...')

You can lookup the full xpatch using inspect in Google Chrome.您可以使用 Google Chrome 中的检查来查找完整的 xpatch。 Your code then becomes:然后您的代码变为:

usbar = driver.find_element_by_xpath('/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input') 

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM