Selenium 发现 element_by_id 不适用于单选按钮 [英] Selenium find element_by_id not working with radio buttons

查看:47
本文介绍了Selenium 发现 element_by_id 不适用于单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直在 Python 中使用 Selenium.我完成了我的代码(当时有效)但突然它选择不再工作.再具体一点:如果我尝试:

driver.find_element_by_id("leasingtrue").click()

它返回给我错误:

selenium.common.exceptions.ElementNotInteractableException:消息:元素 无法滚动到视图中

另一方面,如果我通过 XPath 找到元素,它会按预期工作.错误只发生在radio"类型上,但在每一个类型上都是一致的.

由于完整的代码大约有 600 行长,我不打算将其中的每一个都更改为 XPath.另一个相关资料:由于我正在使用 PyCharm,因此我可以恢复到以前确实有效的版本.但是现在,它也给了我同样的错误.有没有人知道这个错误可能与什么有关?

我正在抓取的网站是:

从用户界面,我们可以看到单选按钮被标签覆盖,所以你不能点击单选按钮来选择切换按钮,而是点击标签来做到这一点.

driver.find_element_by_css_selector("input#leasingtrue + label").click();

So I've been working with Selenium in Python. I finished my code (which worked at the time) but suddenly it chose not to work anymore. To be specific: If I try:

driver.find_element_by_id("leasingtrue").click()

It returns me the error:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="leasingfalse" name="IsLeasing" type="radio"> could not be scrolled into view

On the other hand, if I find the element by its XPath it works like intended. The error only occurs with type "radio" but consistently at every single one.

Since the complete code is about 600 lines long I don't intend to change every single one of these to an XPath. Another relevant information: Since I am working with PyCharm I could just revert back to a version that definitely worked before. But now, it gives me the same error as well. Does anyone have a clue to what this error could be related to?

The website I am scraping is: https://www.comparis.ch/autoversicherung/berechnen?carmake=41. I'm using Python 2.7 with Selenium 3.8 The relevant HTML is:

<div class="item-selectable xsmall-6 columns">

<input data-val="true" data-val-required="Bitte wählen Sie eine Antwort aus." data-vertical-alignment="middle" id="leasingtrue" name="IsLeasing" type="radio" value="true" aria-required="true" aria-invalid="false" aria-describedby="IsLeasing-error">
<label for="leasingtrue">Ja</label>

</div>

解决方案

Because you operated element is not radio button, it's toggle button which implemented by CSS + radio button + label.

From the UI, we can see the radio button is covered by the label, so you can't click the radio button to select toggle button, but to click the label to do that.

driver.find_element_by_css_selector("input#leasingtrue + label").click();

这篇关于Selenium 发现 element_by_id 不适用于单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆