pythonweb-scrapingsplinter

Splinter - How to click a link / button implemented as a div or span element


I am using the Splinter library in Python. I wish to click on a link according to the link text, but this is not working.

The link is included in the source like:

<a href="link here"><span style="color:#000000;">link text here</span></a>

Using 'browser.click_link_by_text('link text here')' gives an error:

splinter.exceptions.ElementDoesNotExist: no elements could be found with link by text "link text here"


Solution

  • One option would be to use developer tools or Firebug to find the xpath of the element you are interested in and then use find_by_xpath based on that

    from splinter import Browser
    
    with Browser() as browser:
        browser.visit("http://www.thisamericanlife.org")
        browser.fill('keys', 'relationships')
        button = browser.find_by_xpath('/html/body/div[2]/div[2]/div[1]/div[2]/div/form/div/input[1]').click()
        print browser.url
    

    This fills in the form (in this case for the search term "relationships") and then navigates to the page http://www.thisamericanlife.org/search?keys=relationships