python - 使用 Python Selenium 单击单选按钮

标签 python selenium selenium-webdriver selenium-chromedriver

我正在尝试使用 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
###############################

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

最佳答案

更通用/健壮的 xpath:-

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

关于python - 使用 Python Selenium 单击单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31673856/

相关文章:

python - 获取上次下载文件的下载链接 - Selenium

java - Selenium webdriver 错误无法在(bamboo)上找到元素

python - 从 BeautifulSoup 页面检索所有信息

python - 错误消息在 Django 中未按预期工作

python - 如何计算 python pandas 中的特定列值?

python 3.x Shutil.copy FileNotFoundError 错误

java - 为什么我关于该元素包含某个字符串的断言失败了,即使它存在?

python - 如何让 Vim 记住 Python 中定义的变量?

python - 如何在 Python 中使用 Selenium 打开 chrome 开发者控制台?

python - selenium 'nonetype' 对象没有属性 'send_keys'