我在Fedora29上使用Selenium 3.12.0和Python3.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
这是我在截图后看到的。你知道吗
请使用
is_selected
检查无线电状态:相关问题 更多 >
编程相关推荐