2
我試圖選擇一個使用Python Selenimum的單選按鈕。我已經嘗試了所有發佈的解決方案,但沒有爲給定的網站工作。我的完整代碼是:用Python Selenium單擊一個單選按鈕
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
###############################
該代碼工作正常,直到它必須選擇單選按鈕。如圖所示,我嘗試了幾種方法來選擇單選按鈕,但似乎沒有任何工作。任何想法歡迎!
這是不可見的。你不能點擊一個不可見的元素。 – LittleQ