python - 我无法使用 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="">
解决方案
您确定要复制 Xpath 还是完整的 Xpath?我只得到类似于/html/body/div[1]/section/main/article/div[2]/div[1]/div/form/div/div[1]/div/label/input
复制完整 Xpath 时的字符串。
推荐阅读
- html - 你能有一个 Style 并把所有的 CSS 代码放在里面吗?
- c++ - 在 C++ 中使用 Windows 生成的对话进行 DPI 缩放?
- autohotkey - 当 F2 重新映射到打开和应用程序时,将 Excel F2 映射到其他按钮不起作用
- database-design - 如何对复合主键进行约束,使其属性总是彼此不同?
- flutter - 如何在导航上处理父小部件的同级
- android - 全屏对话框示例中 Android 的过渡动画
- excel - 满足条件时遍历数组元素并提取分隔值
- html - 在轮播中获取适当大小的品牌徽标
- android - 两个Android进程是否可以具有相同的名称?
- regex - 如何将正则表达式匹配到文本文件?