使用Selenium从元素中提取'href'

时间:2012-12-23 21:18:12

标签: python xpath selenium

这是我目前的XPath:

//table//tr/td/div/div[1]/div/a/@href

它匹配我正在查看的页面上的十个网址。它有以此格式jobs/720800-Associate-Partner-Investment-Consulting-Vancouver-Job-ID-39708.aspx

的十个匹配项

我正在尝试使用selenium.get_text()来提取@href字符串;然而,我的电话正在拉空(注意:没有失败,只是拉空)。我成功地能够在同一页面上的其他元素上拉取字符串。

我已经搜索过,无法找到解决问题的方法 - 有没有人有一些建议?

4 个答案:

答案 0 :(得分:1)

如果我理解正确,问题是该路径有<a href="XXX"> href为空,其他锚href不为空。你只想得到那些非空的href。那么,使用这个表达式:

//table//tr/td/div/div[1]/div/a[@href!=""]/@href

答案 1 :(得分:1)

如果你使用python selenium,这可能有点晚了 (基于你的标签)你可以这样做(如v2.44.0) :

from selenium import webdriver
# set the driver
driver = webdriver.Firefox()
# get the element
elem = driver.find_element_by_xpath('//table//tr/td/div/div[1]/div/a')
# get the attribute value
link = elem.get_attribute('href')

答案 2 :(得分:0)

仅参考Anchor标签,不要参考href属性。一旦我们拥有了所有元素,然后为href元素执行Get_Attribute()....

find_elements_by_xpath("//table//tr/td/div/div[1]/div/a[@href]")
For Loop
print Each_element.Get_Attribute("href")

我希望这会有所帮助......

答案 3 :(得分:0)

试试这个

get_attribute("//table//tr/td/div/div[1]/div/a@href");