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.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92

4 Answers4

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());
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
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();
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.

SteveBeck
  • 118
  • 6
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);