使用硒从元素中提取“href”

2024-04-28 22:32:18 发布

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

这是我当前的XPath:

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

它匹配我正在查看的页面上的10个url。它有十个匹配项,形式是jobs/720800-Associate-Partner-Investment-Consulting-Vancouver-Job-ID-39708.aspx

我试图使用selenium.get_text()来拉@href字符串;但是,我的调用正在拉空(注意:不是失败,只是拉空)。我能够成功地在同一页上的其他元素上拉字符串。

我已经找遍了,找不到任何解决我问题的办法-有人有什么建议吗?


Tags: 字符串divurlpartnertablejobs页面consulting
3条回答

试试这个

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

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

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

如果使用python selenium,这可能有点晚了 (基于你的标签)你可以这样做(作为2.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')

相关问题 更多 >