![](/img/trans.png)
[英]How to extract the href attribute of the first search result using Selenium and Python
[英]How to extract the href attribute of an element using Selenium and Python
我想抓取 www.tab.com.au 的“Racing-Next to Go”部分的HTML中的 URL。
这是 HTML 的摘录:
<a ng-href="/racing/2020-07-31/MACKAY/MAC/R/8" href="/racing/2020-07-31/MACKAY/MAC/R/8"><i ng-
我只想抓取 HTML 的最后一点,这是一个链接,所以:
/racing/2020-07-31/MACKAY/MAC/R/8
我试图通过使用 xpath 来查找元素,但我无法获得我需要的 URL。
我的代码:
driver = webdriver.Firefox(executable_path=r"C:\Users\Harrison Pollock\Downloads\Python\geckodriver-v0.27.0-win64\geckodriver.exe")
driver.get('https://www.tab.com.au/')
elements = driver.find_elements_by_xpath('/html/body/ui-view/main/div[1]/ui-view/version[2]/div/section/section/section/race-list/ul/li[1]/a')
for e in elements:
print(e.text)
可能您想使用.text
的get_attribute
insted 。 文档在这里。
elements = driver.find_elements_by_xpath('/html/body/ui-view/main/div[1]/ui-view/version[2]/div/section/section/section/race-list/ul/li[1]/a')
for e in elements:
print(e.get_attribute("href"))
是的,您可以使用getAttribute(attributeLocator) function 来满足您的要求。
selenium.getAttribute(//xpath@href);
指定您需要知道其 class 的元素的 Xpath。
HTML 中的值/racing/2020-07-31/MACKAY/MAC/R/8
是href
属性的值,而不是innerText
的值。
您需要使用get_attribute("href")
而不是使用text属性,并且有效的代码行将是:
elements = driver.find_elements_by_xpath('/html/body/ui-view/main/div[1]/ui-view/version[2]/div/section/section/section/race-list/ul/li[1]/a')
for e in elements:
print(e.get_attribute("href"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.