1

Have searched through some of the previous questions and have not been able to rectify - I'm a complete newbie to this, so please forgive the ignorance... Trying to select the third 'Radio' button on the page using the following:

from selenium import webdriver from selenium.webdriver.support.ui import Select from selenium.webdriver.common.keys import Keys import smtplib import time date = time.strftime("%d %b %Y" + ", " + "%H:%M%p") print (time.strftime("%d %b %Y" + ", " + "%H:%M%p")) driver=webdriver.Chrome() driver.maximize_window() driver.get("http://www.water.nsw.gov.au/water-licensing/registers") driver.implicitly_wait(10) driver.find_element_by_xpath('// [@id="wizard_tabs"]/div/div[1]/div/ul/li[3]/input').click() 

The result is:

"Message: no such element: Unable to locate element: {"method":"xpath","selector":"//[@id="wizard_tabs"]/div/div[1]/div/ul/li[3]/input"}"

Have tried waiting for longer (500), but it doesn't make a difference. When I 'inspect' the page that has opened, the xpath is still the same. I know there's a simple answer and I'm hoping you internet legends can help!

    1 Answer 1

    1

    A few points of order:

    1. There is no point in implicitly waiting when you can just perform an explicit wait (WebdriverWait.until) on a unique element on that page.

    2. If the webdriver says the element isn't there, then the locator you are looking for is not there, at least not where it is actually looking. iframes can make you think you are looking in one place when really the webdriver is looking in another.

    3. Always confirm your locators in the browser the moment you suspect something is off. Or just every time, it will save all that time you spend running your script, launching your browser, and loading the page every time as you tinker. Go to your browser's dev tools, click on the console tab, and type $$("css selector here") or $x("xpath here") and hit enter. If an element is returned, click the arrow to expand it and hover over what displays. Is it highlighting the desired element on the page? Then and ONLY then are you using a good locator.

    4. If the dev tools console says your locator is good, but python still won't find it - even after a WebdriverWait - find the iframe that is stealing your focus and use webdriver.switch_to(iframe_name) to steal your focus back to the iframe with your element.

    5. Practice less-brittle XPATHs. They can be quite surgical and readable with minimal effort. I would be willing to bet your input has something we could use to find it directly. If not, an element right above or below it in the hierarchy likely does. Post the HTML of your desired element and we'll know for sure.

    To handle an iframe issue:

    iframe_element = driver.find_element_by_css_selector("iframe[name='name_of_the_iframe']") driver.switch_to.frame(iframe_element) # Now find your element 
    2
    • Aphid - Thanks for coming back to me so quickly. As mentioned, I'm new to this (3rd attempt at writing a script), so will try to answer as best I can..! When I copy the outer-HTML, it brings back: <input type="radio" name="what" value="waterSource" data-bind="checked: what"> All the data seems to be held in an iFrame called "Maximum harvestable right calculator" Should I copy and display here the whole outer-HTML of the iFrame?
      – ToddO
      CommentedFeb 25, 2018 at 4:30
    • 1
      Aphid - Thankyou again for your help, have it working the way I want it to now. I'm working to generate an automated report using several website and your feedback here will come in handy in future instances. Not sure I completely understand it all yet, but it will come with practice and time!
      – ToddO
      CommentedFeb 25, 2018 at 11:03

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.