如何找到一个包含href值某些部分的元素?(Python硒)

2024-09-28 05:19:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一些部分的值href(例如,href的查询值)不是一个完整的值。在这种情况下,选择元素的最佳方法是什么(单击、发送键等)?在

我试过很多方法,但效果不太好。特别是,在得到html源代码之后,我使用're'模块,提取了完整的href。然后,我用了webdriver.find_元素(按.XPATH,“//a[@href='the full href']”),但结果是“NoSuchElementError”。 (我假设服务器不断更改href的某些部分)

总之,查找和选择仅包含href值某些部分的元素的最佳方法是什么? (python3.6/硒模块/PhantomJS)


Tags: 模块the方法re元素源代码html情况
3条回答

包含值的Href

//a[contains(@href, 'part/of/href')]

以值开头的Href

^{pr2}$

另一个选项-不按href本身搜索,而是By.linkedText()-可能在java中使用selenium,我不知道在python中是否可能

driver.findElement(By.linkedText("some linked text inside a attribute"));

包含值的Href

driver.find_element_by_partial_link_text('partial href text here')

试试这个:

webdriver.find_element(By.XPATH, "//a[@href='" + the_full_href + "']")

如果the_full_href是一个变量,这将起作用

相关问题 更多 >

    热门问题