python - 从 python 中的 webElement 中查找 xpath
问题描述
我正在尝试使用 python 抓取一些页面,使用 Selenium,但我有一个问题。由于页面结构不同,我正在寻找的元素具有不同的 xpath,但它们都位于页面的同一部分,具有相同的类名。所以我想知道是否有可能,一旦我得到 webElement,知道它的类名,以及我感兴趣的页面部分,找到这部分页面的 XPath。
我这样做是为了找到页面的一部分:
interestedPart = driver.find_element_by_class_name("_45kb")
但是在那之后我不知道如何找到“interestedPart”完整的XPath,你能帮我吗?
完整 XPath 的示例:
/html/body/div[1]/div/div[4...
解决方案
是的,有可能
element.get_attribute('class')
xpath = "//div[@class='"+element.get_attribute('class')+"']"
对于您的示例 Webelement,称为 InterestPart:
xpath = "//div[@class='"+interestedPart.get_attribute('class')+"']"
推荐阅读
- javascript - 由于 Internet 断开连接而失败后重试 AJAX
- mysql - 由于内存泄漏导致 VPS 停机?
- r - 在 R 中提取公共连续序列的函数
- r - 找不到 data.table 中存在的列?
- jquery - 选择表中最后一行id结尾的span
- javascript - Javascript获取DOM元素的索引
- java - Log4j2 动态日志未在运行时创建
- robotframework - 可以在机器人框架中使用的键盘箭头键的ascii值是多少?
- rest-assured - 放心登录测试
- amazon-web-services - 将 MIME 中的附加数据嵌入到电子邮件的 HTML 部分。未链接到附件