SO03112
SO03112

Reputation: 178

selenium xpath doesn't return title for link

I'm trying to print a title from a link, but it doesn't return any values. Can anyone see where I've gone wrong?

Link to HTML for the link I'm trying to get the title from - 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)

Upvotes: 1

Views: 170

Answers (1)

Dean Fenster
Dean Fenster

Reputation: 2395

You need to prepend a . to your xpaths.

An xpath starting with / will search in the root of the current document, instead of within the current element. 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")

Upvotes: 2

Related Questions