gpt4 book ai didi

python - 使用 Firefox WebDriver 单击与同名标签耦合的单选按钮

转载 作者:行者123 更新时间:2023-12-01 08:08:00 24 4
gpt4 key购买 nike

我在 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

最佳答案

请使用is_selected检查 radio 状态:

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())}")

关于python - 使用 Firefox WebDriver 单击与同名标签耦合的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55440816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com