所以我一直在使用Python中的Selenium。我完成了我的代码(这在当时起作用),但突然它选择不再工作了。具体来说:如果我尝试:
driver.find_element_by_id("leasingtrue").click()
它会返回错误:
selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="leasingfalse" name="IsLeasing" type="radio"> could not be scrolled into view
另一方面,如果我通过它的XPath找到元素,它的工作方式就像预期的那样。错误只发生在“收音机”类型上,但在每一个类型上都是一致的。
由于完整的代码大约有600行长,所以我不打算将其中的每一个代码都更改为XPath。另一个相关信息:由于我正在使用PyCharm,所以我可以返回到以前确实有效的版本。但是现在,它也给了我同样的错误。有人知道这个错误可能与什么有关吗?
我正在抓取的网站是:https://www.comparis.ch/autoversicherung/berechnen?carmake=41。我在Selenium 3.8中使用Python2.7,相关的HTML是:
<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>
发布于 2018-02-12 06:02:15
因为您操作的元素不是单选按钮,所以它是切换按钮,由CSS + radio button + label
实现。
从UI中可以看到单选按钮被标签覆盖,所以您不能单击单选按钮来选择切换按钮,而是单击标签来实现这一点。
driver.find_element_by_css_selector("input#leasingtrue + label").click();
发布于 2018-02-12 04:14:44
尝试以下代码行:
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "leasingfalse")))
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()
希望它能帮到你!
https://stackoverflow.com/questions/48746229
复制相似问题