简体   繁体   中英

Element not click-able (Selenium Webdriver - JAVA)

All,

I have been working with Selenium webdriver for some time and I got into a strange problem. I need to click an element which selenium webdriver is not doing so all of my next steps fail. The html of the element is as follows.

<div id="group-container" class="grp-view-container">

<div id="group-container-0" class="component-inline-block" '="" data-original-title="" title="">

<div id="group-container-1" class="component-inline-block" '="" data-original-title="" title="">

</div>

I need to click on group-container-1. I have used a simple click, Actiions, JavascriptExecutor, SendKeys(keys.RETURN).

I used Chromedriver, FirefoxDriver.

Please guide me.

Thanks.

After some google search and local test, I came to the conclusion that indeed if and element is hidden behind another element, the normal element.click() call from selenium won't work.

In your case, you probably have some css that hides the you element behind other elements, thus making it unreachable for Selenium.

In this case you should use plain old JS.

Here is an example with FireFoxDriver:

FirefoxDriver driver = new FirefoxDriver();
driver.get("ENTER YOU URL HERE");
// driver.findElement(By.id("group-container-1")).click(); This will work only if the element is not hidden.

String jsExpression = "document.getElementById('group-container-1').click();";
((JavascriptExecutor) driver).executeScript(jsExpression);

To see any result you must have onClick listener on the clicked element though.

This sort of situation often arises when you have CSS that make use of the z-index. A transparent portion of another HTML element maybe covering the HTML element of interest to you.

Sometimes the same situation may arise when you use absolute positioning.

I have had the same issue in the past when I was writing a script to test a search field. The script worked on the page header but not the footer. I could also test the field by key entry ( sendKeys ) just not mouse click.

I got this error:

org.openqa.selenium.ElementClickInterceptedException: element click intercepted: Element <input type="submit" class="submit hidden-text" value="Search"> is not clickable at point (988, 633). Other element would receive the click: <i class="fas fa-arrow-circle-up"></i>

My ScreenShotOnFailure.java utility showed the problem: 返回顶部按钮遮挡搜索图标

A button taking the using 'back to the top' of the page partially obscured the search icon I wanted to click. I initially tried to get around this by specifying the window size ( driver.manage().window().setSize(new Dimension(1024, 768)); ) as I assumed it was being obscured due to the browser dimensions but that didn't help. So I made the switch to JS to click the button.

I changed:

driver.findElement(By.xpath("(//input[@value='Search'])[2]")).click();

to:

org.openqa.selenium.WebElement ele = driver.findElement(By.xpath("(//input[@value='Search'])[2]"));
    JavascriptExecutor executor = (JavascriptExecutor)driver;
    executor.executeScript("arguments[0].click();", ele);

(and imported the relevant imports)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM