首页 > 解决方案 > 我无法使用 xpath 或 CSS 类在 HTML 页面中定位元素

问题描述

我尝试了很多次复制元素的xpath,它没有给我像下面这样的 xpath:

//*[@id="search_form_input_homepage"]

相反,它给了我如下内容:

/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input

我什至尝试使用 CSS 类和名称来定位元素,但是每当我使用Python提交代码时,我仍然会收到此错误。

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: /html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input

这是我想找到它们的 xpath 的 HTML 元素:

<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="">

<input aria-label="Password" aria-required="true" autocapitalize="off" autocorrect="off" name="password" type="password" class="_2hvTZ pexuQ zyHYP" value="">

标签: pythonpython-3.xseleniumselenium-webdriverselenium-firefoxdriver

解决方案


您确定要复制 Xpath 还是完整的 Xpath?我只得到类似于/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input复制完整 Xpath 时的字符串。


推荐阅读