2

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>
Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
  • Possible duplicate of [Selenium WebDriver throws "Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element is not visible" error](https://stackoverflow.com/questions/44690971/selenium-webdriver-throws-exception-in-thread-main-org-openqa-selenium-elemen) – undetected Selenium Feb 12 '18 at 12:15
  • I already implemented a sleep timer, doesnt work either. – user9349795 Feb 12 '18 at 12:51
  • `ElementNotInteractableException` have more to do with _Temporary Overlay_ and _Permanent Overlay_. Please go through the discussions properly. – undetected Selenium Feb 12 '18 at 12:55
  • Well i am sorry, but i do not see on how this could be related to the fact that it is accessible with XPath and not with the id or name, or how this would explain the fact that it worked properly up until today morning. Please give me some insight on your idea regarding the context. – user9349795 Feb 12 '18 at 13:23
  • To enable the SO volunteers to help you with a accurate and working solution you must consider updating the question with the version info of the binaries you are using, relevant _HTML_ along with your code trials and error stack trace. – undetected Selenium Feb 12 '18 at 13:28
  • Still your _HTML_ consists of `id="leasingtrue"` where as your code trial consists of `id("leasingfalse")` – undetected Selenium Feb 12 '18 at 13:48

2 Answers2

3

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

enter image description here

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();
jww
  • 97,681
  • 90
  • 411
  • 885
yong
  • 13,357
  • 1
  • 16
  • 27
  • Thank you, that clears it up. But since the code was working the weeks before does that mean they changed the html itself? (Sorry i am quite new to this..) – user9349795 Feb 12 '18 at 14:40
1

Try the following lines of code:

element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "leasingfalse")))
driver.execute_script("arguments[0].scrollIntoView(true);", element)
element.click()

Hope it helps you!

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40