0

I am trying to click a toggle with Selenium Webdriver and Python but get the following exception:

 selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
                           (Session info: chrome=86.0.4240.111)

My HTML code

<div _ngcontent-c7="" class="form-group">
  <label _ngcontent-c7="" for="opened">Opened</label>
 <div _ngcontent-c7="" class="toggle-switch">
    <input _ngcontent-c7="" id="opened" name="opened" type="checkbox" class="ng-untouched ng-pristine ng-valid">
  <label _ngcontent-c7="" for="opened"></label>
 </div>
 <span _ngcontent-c7="" class="form-control-helper">Toggle text description.</span>
</div>

I use the following selector and it is 100% unique:

.toggle-switch>label[for=opened]

What can be the issue? This is the Angular modal dialog. I tried many unique locators, both XPATH and CSS and I am sure this is not a locator problem. Did someone else had a similar problem?

My Python code:

    toggle = driver.find_element_by_css_selector(".toggle-switch>label[for=opened]")
    toggle.click()

Thanks.

2 Answers 2

1

you're trying to interact with the label. you want the input element:

 #remove the label from page
 driver.execute_script("document.querySelector(\"strong[for='opened']\").style.visibility = 'hidden';")

 toggle = driver.find_element_by_css_selector("#opened")
 toggle.click()
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you for your help. I tried it but got the error: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <input _ngcontent-c6="" id="opened" name="opened" type="checkbox" class="ng-untouched ng-pristine ng-valid"> is not clickable at point (621, 448). Other element would receive the click: <label _ngcontent-c6="" for="opened"></label> (Session info: chrome=86.0.4240.111)
I added debug("Element is visible? " + str(toggle.is_displayed())) and see False. It means the element is not found by selenium.
ahh the label is styled to overlay the input some how. Ok.
I updated to remove the label overlay. A Pain, but may be neccesary.
I messed up my strings.
|
1

The best bulletproof option which I use now is:

toggle = driver.find_element_by_css_selector(".toggle-switch>label[for=opened]")
driver.execute_script("arguments[0].click();", toggle)

In addition, sometimes I need to wait until an element that covers my toggle becomes not visible with:

 wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, "my css selector")))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.