如何找到具有href值的某些部分的元素? (蟒蛇硒)

时间:2017-06-28 11:45:14

标签: python selenium xpath element

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

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

无论如何,只有href值的某些部分才能找到并选择元素的最佳方法是什么? (python3.6 / selenium module / PhantomJS)

3 个答案:

答案 0 :(得分:0)

包含值的Href

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

以值

开头的Href
//a[starts-with(@href, 'http://start.of.href.')]

另一种选择 - 不是通过href本身搜索,而是By.linkedText() - 在带有selenium的java中可能,我在python中不知道是否可能

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

答案 1 :(得分:0)

试试这个:

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

如果the_full_href是变量

,这将有效

答案 2 :(得分:0)

包含值的Href

driver.find_element_by_partial_link_text('partial href text here')