问题描述
我在Fedora 29上使用Selenium 3.12.0,Python 3.7.2和Firefox 66.0.1.我在单击单选按钮时遇到问题.单选按钮在标签内,单选和标签使用相同的名称.该页面位于 https://complaints.donotcall.gov/complaint/complaintcheck.aspx.
I'm using Selenium 3.12.0 with Python 3.7.2 and Firefox 66.0.1 on Fedora 29. I'm having trouble clicking a radio button. The radio button is inside a label, and the radio and label use the same name. The page is located at 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>
在页面完成后查看屏幕截图时,我看到未单击单选按钮.页面上的其他元素都已完成,可以.
When I examine a screenshot after the page has been completed, I see the radio buttons are not clicked. The other elements on the page are completed OK.
我尝试了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)
也无济于事.
I've tried driver.find_element_by_id("PrerecordMessageYESRadioButton")
, driver.find_element_by_name("PrerecMsg")
and driver.find_element_by_css_selector("input#PrerecordMessageYESRadioButton")
. Once selected, I've also tried radio.click()
, radio.send_keys(Keys.ENTER)
, and radio.send_keys(Keys.SPACE)
with no joy. Finally, driver.execute_script("arguments[0].click();", radio)
was not helpful, either.
在这种情况下,如何单击与标签耦合的单选按钮?
How does one click the radio button coupled to a label in this case?
单选按钮似乎会引起很多麻烦.这是一些相关的问题,但是在这种情况下它们没有帮助.第一次参考和@yong的回答似乎与这个问题非常相关.
Radio buttons seem to cause a fair amount of trouble. Here are some related questions, but they did not help in this instance of the problem. The first reference and @yong's answer seems very relevant to this problem.
- 硒发现element_by_id无法与单选按钮一起使用
- 如何使用Selenium和Python选择单选按钮
- 在Python中使用Selenium单击/选择单选按钮
- 无法单击Selenium Webdriver中的单选按钮
- Selenium find element_by_id not working with radio buttons
- How to select radio button with Selenium and Python
- Using Selenium in Python to click/select a radio button
- Unable to click on a radio button in Selenium Webdriver
这是测试脚本:
$ 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枚举页面上的元素:
Enumerating the elements on the page by 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
这是抓取屏幕截图后看到的内容.
Here is what I am seeing after fetching the screenshot.
推荐答案
请使用is_selected
检查无线电状态:
Please check radio status using 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())}")
这篇关于单击单选按钮并使用Firefox WebDriver耦合到具有相同名称的标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!