简体   繁体   English

使用 Selenium 中的 Python 到 select 一个单选按钮

[英]Using Selenium in Python to select a radio button

I am trying to make a bot that automates booking a study room (Microsoft booking).我正在尝试制作一个自动预订自习室的机器人(Microsoft 预订)。 When I try to select the radio button, it doesn't work.当我尝试 select 单选按钮时,它不起作用。 I tried using id and CSS_SELECTOR.我尝试使用 id 和 CSS_SELECTOR。 I appreciate any help you can provide.感谢您提供的任何帮助。

I want select room C, ID:service_2我要select房间C,ID:service_2

room_c_select=driver.find_element(By.ID, "service_2")
room_c_select.click()

driver.find_element(By.CSS_SELECTOR,"input#service_2").click()

driver.find_element(By.CSS_SELECTOR,"[class='right serviceCard']input[type='radio'][id='service_2']")[0].click()

You have to click on the small circle, ie你必须点击小圆圈,即

driver.find_elements(By.CSS_SELECTOR, 'span.image.icon-circleRegular')[1].click()

Using find_elements() to locate any specific element isn't a part of the best practices as demonstrated by the other answer.使用find_elements()定位任何特定元素并不是其他答案所证明的最佳实践的一部分。

As per the html published as a comment, to click on the associated with the text Group Study Room C you can use the following locator strategy :根据作为评论发布的html ,要单击与文本Group Study Room C关联的,您可以使用以下定位器策略

driver.find_element(By.XPATH, "//div[@class='name line-clamp' and text()='Group Study Room C']//ancestor::label[1]").click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM