2

I am trying to select a radio button using Python Selenimum. I have already tried all solutions posted yet nothing is working for the given website. My full code is:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

baseDomain = "https://www.budgetdirect.com.au"
startUrl = baseDomain + "/start/home.html"
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches",["ignore-certificate-   errors"])
browser = webdriver.Chrome(chrome_options=options)
browser.get(startUrl)
#Selects the start date
el = browser.find_element_by_id('service_response_policy_policyBase_commencementDate')
for option in el.find_elements_by_tag_name('option'):
    if option.text == '28/07/2015':
        option.click()

#Selects the insurance type
el = browser.find_element_by_id('service_response_home_other_summarisedCoverType')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'Home':
        option.click()
time.sleep(1)

#Inserts Post code
inputElement = browser.find_element_by_id("service_response_icAddress_postCode")
inputElement.send_keys('2000')
time.sleep(1)
inputElement.send_keys(Keys.ENTER) # simulates selecting the enter key

#inserts street address
inputElement = browser.find_element_by_id("service_response_icAddress_streetSearch")
inputElement.send_keys('161 Kent Street')
time.sleep(1)
inputElement.send_keys(Keys.TAB) # simulates selecting the enter key
#inputElement.send_keys(Keys.ENTER)
time.sleep(1)

#Selects the ownership status
el = browser.find_element_by_id('service_response_home_occupancy_ownership_residenceOccupancyStatus')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'Owner Occupied':
        option.click()
time.sleep(1)

#Selects the year of moving in
el = browser.find_element_by_id('service_response_home_occupancy_ownership_yearMovedIn')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'More than 5 years':
        option.click()
time.sleep(1)

#Selects the home type
el = browser.find_element_by_id('service_response_transactionData_homeType')
for option in el.find_elements_by_tag_name('option'):
    if option.text == 'Freestanding Home':
        option.click()

####PROBLEM WITH CODE HERE####

#Ticks no to body corporate
browser.find_elements_by_xpath('.//input[@type="radio" and @value="N"]')[0].click # Unsuccessful to select
browser.find_element_by_id('service_response_home_homeFeatures_bodyCorporateStrataTitle_N').click() # Unsuccessful to select

for i in browser.find_elements_by_xpath('//*[@type="radio"]'): # Unsuccessful to select
    try:
        i.click()
    except:
        pass
###############################

The code works fine until it has to select the radio button. As illustrated, I have tried several methods to select the radio button but nothing appears to work. Any ideas welcome!

1
  • it's invisible. You can NOT click an element which is invisible. Commented Jul 28, 2015 at 10:47

2 Answers 2

3

More generic/robust xpath:-

el = browser.find_element_by_xpath("//label[@for='service_response_home_homeFeatures_bodyCorporateStrataTitle_N']");
el.click();
Sign up to request clarification or add additional context in comments.

1 Comment

Have tried, but without success. Do you know another solution? I'm trying to click in a radio button in surveymonkey form.
0

Try this code:

el = browser.find_element_by_xpath("//*[@id='panel-2-body']/div[1]/div[2]/div[3]/div[2]/div/div/label[2]")
el.click()

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.