xpath 仅在 Selenium / Python 中的 WebElement 内检查
我对此非常陌生,我试图寻找答案,但找不到任何答案。
我正在使用Selenium+Chromedriver,试图监视我感兴趣的一些项目。
例如: 列表中有20个项目的页面。
代码: 页面上的项目列表
search_area = driver.find_elements_by_xpath(“ // li [@data-testid ='test']”)
search_area [19] .find_element_by_xpath(“ // p [@class ='sc-hkwdye name'sc-hkwdye name'sc-hkwdye name'''' ]
- )
。
- >
XPath为什么要查看父html?
我希望Xpath在WebElement /List项目中返回项目名称。是否可以?
I am very new to this and i have tried to look for the answer to this but unable to find any.
I am using Selenium+chromedriver, trying to monitor some items I am interested in.
Example:
a page with 20 items in a list.
Code:
#list of items on the page
search_area = driver.find_elements_by_xpath("//li[@data-testid='test']")
search_area[19].find_element_by_xpath("//p[@class='sc-hKwDye name']").text
- this returns the name of item[0]
search_area[19].find_element_by_css_selector('.name').text
- this returns the name of item[19]
why is xpath looking at the parent html?
I want xpath to return the name of item within the WebElement /list item. is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到答案,添加一个。希望
这将帮助将来像我这样的新人。
从
search_area [19] .find_element_by_xpath(“ // p [@class ='sc-hkwdye name']
)
search_area [19] .find_element_by_xpath(“ 。 // p [@class ='sc-hkwdye name'])
found the answer, add a . in front
hope this is gonna help someone new like me in the future.
from
search_area[19].find_element_by_xpath("//p[@class='sc-hKwDye name']").text
to
search_area[19].find_element_by_xpath(".//p[@class='sc-hKwDye name']").text
您在
find_element_by_xpath中传递的内容(“ // p [@class ='sc-hkwdye name']”)
是相对xpath。您可以通过完整的XPATH获得理想的结果。What you are passing in
find_element_by_xpath("//p[@class='sc-hKwDye name']")
is relative Xpath. You can pass the full Xpath to get the desired result.