单击单选按钮,并使用Firefox WebDriver将其与具有相同名称的标签耦合在一起

时间:2019-03-31 12:18:50

标签: python selenium selenium-webdriver radio-button

我在Fedora 29上使用Selenium 3.12.0,Python 3.7.2和Firefox 66.0.1。我在单击单选按钮时遇到问题。单选按钮位于标签内,单选和标签使用相同的名称。该页面位于https://complaints.donotcall.gov/complaint/complaintcheck.aspx

<label for="PrerecordMessageYESRadioButton">
    <input id="PrerecordMessageYESRadioButton" type="radio" name="PrerecMsg" value="PrerecordMessageYESRadioButton" tabindex="7">
    <label for="PrerecordMessageYESRadioButton">Yes</label>
</label>

在页面完成后查看屏幕截图时,我看到未单击单选按钮。页面上的其他元素都已完成,可以。

我尝试了driver.find_element_by_id("PrerecordMessageYESRadioButton")driver.find_element_by_name("PrerecMsg")driver.find_element_by_css_selector("input#PrerecordMessageYESRadioButton")。选定之后,我也没有尝试过radio.click()radio.send_keys(Keys.ENTER)radio.send_keys(Keys.SPACE)。最后,driver.execute_script("arguments[0].click();", radio)也没有帮助。

在这种情况下,如何单击与标签耦合的单选按钮?


单选按钮似乎会引起很多麻烦。这是一些相关的问题,但是在这种情况下它们没有帮助。第一个参考资料和@yong的答案似乎与此问题相关。


这是测试脚本:

$ cat test-driver.py
#!/usr/bin/env python3

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.options import Options

def main():

    opts = Options()
    opts.headless = True
    driver = webdriver.Firefox(options=opts)   

    #################################################

    print("Fetching page 1")    
    driver.get("https://complaints.donotcall.gov/complaint/complaintcheck.aspx")

    print("Clicking Continue")
    button_continue = driver.find_element_by_id("ContinueButton")
    button_continue.click()

    #################################################

    print("Fetching page 2")    
    time.sleep(2) 

    text_phone = driver.find_element_by_id("PhoneTextBox")
    for ch in "8005551212":
        text_phone.send_keys(ch)

    text_calendar = driver.find_element_by_id("DateOfCallTextBox")
    for ch in "03/30/2019":
        text_calendar.send_keys(ch)

    dropdown_hour = driver.find_element_by_id("TimeOfCallDropDownList")
    dropdown_hour.send_keys("10")

    dropdown_minute = driver.find_element_by_id("ddlMinutes")
    dropdown_minute.send_keys("30")

    # PrerecordMessageYESRadioButton
    radio_robocall = driver.find_element_by_name("PrerecMsg")
    # radio_robocall = driver.find_element_by_css_selector("input#PrerecordMessageYESRadioButton")
    radio_robocall.send_keys(Keys.ENTER)
    radio_robocall.send_keys(Keys.SPACE)
    ...

    driver.quit()


if __name__ == "__main__":
    main()

按id枚举页面上的元素:

ids = driver.find_elements_by_xpath('//*[@id]')
for val in ids:
    print(val.get_attribute('id'))

返回以下内容:

Head1
_fed_an_ua_tag
bdyComplaint
top
changeLang
topnav
navbtn
mobileChangeLang
Form1
__EVENTTARGET
__EVENTARGUMENT
__VIEWSTATE
__VIEWSTATEGENERATOR
__EVENTVALIDATION
StepOnePanel
StepOneEntryPanel
ErrorMsg
PhoneTextBox
DateOfCallTextBox
TimeOfCallDropDownList
ddlMinutes
PrerecordMessageYESRadioButton
PrerecordMessageNORadioButton
PhoneCallRadioButton
MobileTextMessageRadioButton
ddlSubjectMatter
spnTxtSubjectMatter
txtSubjectMatter
StepOneContinueButton
hdnBlockBack
hdnPhoneChecked
hdnCompanyChecked
hdnPhoneNumber

这是抓取屏幕截图后看到的内容。

enter image description here

1 个答案:

答案 0 :(得分:1)

请使用is_selected检查广播状态:

radio_robocall = driver.find_element_by_name("PrerecMsg")
# is_selected should return False
print(f"radio_robocall status: {str(radio_robocall.is_selected())}")

radio_robocall.click()
# is_selected should return True
print(f"radio_robocall status: {str(radio_robocall.is_selected())}")