2

I am trying to click on an element with Selenium. It is a checkbox with HTML Code below:

Trying to click Checkbox for terms

Code for paragraph that contains checkbox and Terms:

<p class="jss72 jss80 jss927 jss943 jss930">
    <span class="js122">
        <img src="/images/purple.svg">
    </span>
    <span class="jss941">
        I Agree To The Terms
    </span>
</p>

I have tried three different ways, but none of them is working:

//Tried with xpath clicking on image: Error: Element not interactable   
driver.FindElement(By.XPath("//img[@src='/images/purple.svg']")).Click();

//Tried with Xpath by selecting the span 
driver.FindElement(By.XPath("//span[@class='js122')]")).Click();

//Tried with CssSelector Error: Element not interactable  
driver.FindElement(By.CssSelector("img[src*='purple.svg']")).Click();

Please help with any other workaround.

5
  • sometimes need done before click event onmouseover Commented Feb 3, 2021 at 5:05
  • It is a checkbox, what is the workaround with onmouseover. Please help Commented Feb 3, 2021 at 5:14
  • here java, but can understand and for c# stackoverflow.com/questions/17293914/… Commented Feb 3, 2021 at 5:37
  • Can you provide site, or more HTML code? And put the error log also, did you get "No such element" or stale element exption, or something else? Commented Feb 3, 2021 at 7:45
  • Yes I am getting the error "Element Not Interactable". I am not able to work with span class as its dynamic.. I have editted the question, please check Commented Feb 3, 2021 at 8:24

4 Answers 4

0

The ElementNotInteractableException happens due to a race condition between Selenium executing your code, and the browser executing JavaScript on the page. The element you want to click exists in a flyover. I bet there is a fade-in animation occurring when the flyover appears. There is a brief moment in time where you can visibly see the checkbox, and Selenium can find it in the DOM, but Selenium cannot click on it. The solution is pretty simple. Use an explicit wait:

var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

// The 'd' parameter to the lambda expression is an IWebDriver object
wait.Until(d => d.FindElement(By.XPath("//img[@src='/images/purple.svg']")).Click());
Sign up to request clarification or add additional context in comments.

1 Comment

still the same error "Element not Interactable", even with the code.
0

Before try to finding the element you need to switch that frame or pop-up.

to do this ;

driver.switchTo().frame(<yourFrameIndex>);

or

driver.switchTo().activeElement();

1 Comment

I am able to fill the entire form except for the check box....
0

I do not see a checkbox in the html you shared:

<p class="jss72 jss80 jss927 jss943 jss930">
    <span class="js122">
        <img src="/images/purple.svg">
    </span>
    <span class="jss941">
        I Agree To The Terms
    </span>
</p>

I would expect to see something like

<input type='checkbox'/>

Rather then the paragraph which is the text next to the checkbox.

Comments

0

I tried every answer here, but no luck. I found the solution myself. I tried the DOM method.

IWebElement we = driver.FindElement(By.CssSelector("img[src*='purple.svg']"));

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("arguments[0].click();", we);

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.