Mayank Yadav
Mayank Yadav

Reputation: 33

Unable to click on Web Element using Selenium with Java

I tried WebDriverWait, .click(), .sendKeys(Keys.RETURN), implicitWait, explicitWait and many more methods, but I'm unable to click on this web element.

<div class="actions style-scope tv-overlay-record-on-the-go">
  <tv-button pill-action="" class="style-scope tv-overlay-record-on-the-go x-scope tv-button-2 left"><button class="style-scope tv-button"><div class="wrapper style-scope tv-button"><iron-icon id="icon" class="style-scope tv-button x-scope iron-icon-0"></iron-icon><div id="content" role="presentation" class="style-scope tv-button">Got it</div></div></button>
    <div
      class="hover-hint style-scope tv-button"> </div>
</tv-button>
</div>

Based off the above HTML code, I created the following xpath:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

gotIt.click();

I believe the code runs and acknowledges that the button is there, and thus the web element is created successfully. But, when I try to interact with it by using the many interaction methods, nothing happens.

enter image description here

Exception I got: org.openqa.selenium.ElementNotInteractableException: element not interactable

Upvotes: 1

Views: 195

Answers (3)

CEH
CEH

Reputation: 5909

I would try querying on the text rather than just ID attribute of div -- your XPath might be returning many results, and clicking on the first one, which is why nothing happens.

I would try this:

WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[text()='Got it']"));

//gotIt.click(); throws element not interactable

// you can also try Javascript click -- work around element not interactable issue
((JavascriptExecutor)driver).executeScript("arguments[0].click();", gotIt);

I changed your XPath to query on the 'Got it' text rather than the ID attribute content which may be returning multiple results. I also included code to execute a Javascript click, which may help as a workaround to any click issues.

The Xfinity website seems to have many iframe elements, but I can't get to your specific page to check and see, so this may or may not be causing an issue as well.

Upvotes: 1

sayhan
sayhan

Reputation: 1184

Please try Action Object for clicking like following :

    WebElement gotIt = driver.findElement(By.xpath("//div[@class='actions style-scope tv-overlay-account-active']//div[@id='content']"));

    Actions actions = new Actions(driver);
    actions.moveToElement(gotIt).click().build().perform();

Upvotes: 0

SeleniumUser
SeleniumUser

Reputation: 4177

Check your button isn't on the iframe if not then you can find below XPath using contains to click on the Got it button

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
element1.click();

Solution 2:

WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[contains(text(),'Got it')]")));
Actions builder = new Actions(driver);
Action buttonClick = builder.moveToElement(element1).click().build();

Upvotes: 0

Related Questions