繁体   English   中英

硒xpath不返回链接标题

[英]selenium xpath doesn't return title for link

我正在尝试从链接中打印标题,但它不返回任何值。 谁能看到我哪里出问题了?

我要从中获取标题的链接的HTML链接-http://imgur.com/a/niTAs

driver.get("http://www.theflightdeal.com/category/flight-deals/boston-flight-deals/")

results = driver.find_elements_by_xpath('//div[@class="post-entry half_post half_post_odd"]')


for result in results:
    main = result.find_element_by_xpath('//div[@class="entry-content"]')
    title1 = main.find_element_by_xpath('//h1/a')
    title = title1.get_attribute('title')
    print(title)

您需要在前面添加一个. 到你的xpath。

/开头的xpath将在当前文档的根目录中搜索,而不是在当前元素中搜索。 请参阅功能文档

  This will select the first link under this element. :: myelement.find_elements_by_xpath(".//a") However, this will select the first link on the page. :: myelement.find_elements_by_xpath("//a") 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM