首页 > 解决方案 > Python Selenium 获取属性“href”错误

问题描述

我正在尝试从链接中获取 href,请找到我的代码:

url ='http://money.finance.sina.com.cn/bond/notice/sz149412.html'
link = driver.find_element_by_xpath("//div[@class='blk01'])//ul//li[3]//a[contains(text(),'发行信息']").get_attribute('href')
print(link)

错误

 invalid selector: Unable to locate an element with the xpath expression 
SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//div[@class='blk01'])//ul/li[3]//a[contains(text(),'发行信息']' is not a valid XPath expression.

似乎它不是有效的 xpath,但我无法找出错误,任何帮助将不胜感激!

谢谢

标签: pythonselenium

解决方案


试试这个:

link = driver.find_element_by_xpath('//div[@class="blk01"]//ul//li[3]//a[contains(text(), "发行信息")]')
print(link.get_attribute("href"))



推荐阅读