1

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)

1 Answer 1

2

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.