单击带有 Python Selenium 的单选按钮

Click a radio button with Python Selenium

我正在尝试 select 使用 Python Selenimum 的单选按钮。我已经尝试了所有发布的解决方案,但对给定的网站没有任何效果。我的完整代码是:

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
###############################

代码工作正常,直到它必须 select 单选按钮。如图所示,我尝试了几种方法来 select 单选按钮,但似乎没有任何效果。欢迎任何想法!

试试这个代码:

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

更多 generic/robust xpath:-

el = browser.find_element_by_xpath("//label[@for='service_response_home_homeFeatures_bodyCorporateStrataTitle_N']");
el.click();