简体   繁体   English

元素不可点击(Selenium Webdriver - JAVA)

[英]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.我已经使用 Selenium webdriver 一段时间了,但遇到了一个奇怪的问题。 I need to click an element which selenium webdriver is not doing so all of my next steps fail.我需要单击 selenium webdriver 没有执行的元素,因此我接下来的所有步骤都失败了。 The html of the element is as follows.元素的html如下。

<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.我需要点击 group-container-1。 I have used a simple click, Actiions, JavascriptExecutor, SendKeys(keys.RETURN).我使用了简单的点击、Actions、JavascriptExecutor、SendKeys(keys.RETURN)。

I used Chromedriver, FirefoxDriver.我使用了 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.经过一些谷歌搜索和本地测试,我得出的结论是,如果和元素确实隐藏在另一个元素后面,则来自 selenium 的正常 element.click() 调用将不起作用。

In your case, you probably have some css that hides the you element behind other elements, thus making it unreachable for Selenium.在您的情况下,您可能有一些 css 将 you 元素隐藏在其他元素后面,从而使 Selenium 无法访问它。

In this case you should use plain old JS.在这种情况下,您应该使用普通的旧 JS。

Here is an example with FireFoxDriver:这是 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.要查看任何结果,您必须在单击的元素上具有 onClick 侦听器。

This sort of situation often arises when you have CSS that make use of the z-index.当您拥有使用 z-index 的 CSS 时,经常会出现这种情况。 A transparent portion of another HTML element maybe covering the HTML element of interest to you.另一个 HTML 元素的透明部分可能覆盖了您感兴趣的 HTML 元素。

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.我还可以通过键输入( sendKeys )来测试该字段,而不是鼠标单击。

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:我的 ScreenShotOnFailure.java 实用程序显示了这个问题: 返回顶部按钮遮挡搜索图标

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.我最初试图通过指定窗口大小( driver.manage().window().setSize(new Dimension(1024, 768)); )来解决这个问题,因为我认为它由于浏览器尺寸而被遮挡,但事实并非如此没有帮助。 So I made the switch to JS to click the button.所以我切换到JS点击按钮。

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) (和进口相关进口)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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