selenium xpath不返回链接的标题

时间:2017-04-27 16:09:57

标签: python selenium xpath

我正在尝试从链接打印标题,但它不会返回任何值。谁能看到我出错的地方?

链接到我正在尝试从 - http://imgur.com/a/niTAs

获取标题的链接的HTML
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)

1 个答案:

答案 0 :(得分:2)

您需要在{x}之前添加.

/开头的xpath将搜索当前文档的根目录,而不是当前元素。 See function docs

    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")