简体   繁体   English

Selenium - 元素在点上不可点击

[英]Selenium - Element is not clickable at point

I am using selenium for test script. 我正在使用selenium来测试脚本。 I am getting following error and this error randomly occur. 我收到以下错误,随机发生此错误。 When I run 10 times, I get this about twice. 当我跑10次时,我得到了大约两次。 So it's not really reproducible. 所以它不是真正可重复的。 Does anyone know why this is happening? 有谁知道为什么会这样? the element I am trying to click is definitely visible in the browser and doesn't move around so there is no need to resize or drag element. 我试图点击的元素在浏览器中肯定是可见的并且不会移动,因此不需要调整大小或拖动元素。 I am using chrome webdriver and I read other troubleshooting strategies( Debugging "Element is not clickable at point" error ) and they don't seem relevant to my issue. 我正在使用chrome webdriver并且我阅读了其他故障排除策略( 调试“元素在点上无法点击”错误 )并且它们似乎与我的问题无关。 I waited enough time as well. 我也等了足够的时间。

UnknownError: unknown error: Element is not clickable at point (167, 403). Other element would receive the click: <div class="leftMasterBackground"></div>

There are a number of steps you can do in order to improve the stability while clicking on different UI elements: 为了在点击不同的UI元素时提高稳定性,您可以执行许多步骤:

  • Explicitly wait for it's presence in the DOM 明确地等待它在DOM中的存在
  • Scroll into the element view 滚动到元素视图中
  • Check if clickable 检查是否可点击

Does it helped the stability? 它有助于稳定吗?

WebDriverWait wait = new WebDriverWait(driver, 3)
JavascriptExecutor js = ((JavascriptExecutor) driver)

//presence in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

//scrolling
WebElement element = driver.findElement(By.id("ID")));  
js.executeScript("arguments[0].scrollIntoView(true);", element);

//clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));

Further, if you will decide to override the default Actions interface with more customized one, you can use two type of clicks (for example): click() which will have all those stability steps and fastClick() which will be the default clicking without any varification. 此外,如果您决定使用更多自定义的操作界面覆盖默认操作界面,则可以使用两种类型的点击(例如): click()将具有所有这些稳定性步骤,而fastClick()将是默认点击而不使用任何变化。

I have solved by catching the exception and managing it like this: 我通过捕获异常并像这样管理它来解决:

        WebDriver driver = new ChromeDriver();
        WebElement element = driver.findElement(By.id("ID"));
        boolean clicked = false;
        do{
            try {
                element.click();
            } catch (WebDriverException e) {
                continue;
            } finally {
                clicked = true;
            }
        } while (!clicked);

I was also facing same problem with Chrome. 我也遇到了与Chrome相同的问题。 I have solved that putting one line of code before clicking on the element: 我已经解决了在点击元素之前放置一行代码:

scrollToViewElement(driver,xpath);

click parent element of the element which you want to click. 单击要单击的元素的父元素。 this can be workaround solution only. 这可能只是解决方案。

  • This happens only on chrome so it works on ie and firefox 这只发生在chrome上,所以它适用于ie和firefox
  • ChromeDriver always clicks the middle of the element ChromeDriver始终会点击元素的中间部分
  • The reason Chrome driver doesn't calculate the correct screen location of link. Chrome驱动程序无法计算链接的正确屏幕位置的原因。

Solution: 解:

// Find an element and define it

WebElement elementToClick = driver.findElement(By.xpath("some xpath"));
// Scroll the browser to the element's Y position
((JavascriptExecutor) driver).executeScript("window.scrollTo(0,"+elementToClick.getLocation().y+")");
// Click the element
elementToClick.click();

I got the same issue due to one of spinner was hiding the element. 由于其中一个旋转器隐藏了元素,我得到了同样的问题。

I gave xpath and it resolved the issue. 我给了xpath,它解决了这个问题。 Other people suggested to 1. scroll 2. sleep also worked for them. 其他人建议1.滚动2.睡觉也为他们工作。

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

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