Selenium Web 驱动程序和 Java。元素在 (x, y) 点不可点击。其他元素将收到点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44912203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:47:11  来源:igfitidea点击:

Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click

javaseleniumselenium-webdriverwebdriver

提问by Maria

I used explicit waits and I have the warning:

我使用了显式等待,但有警告:

org.openqa.selenium.WebDriverException: Element is not clickable at point (36, 72). Other element would receive the click: ... Command duration or timeout: 393 milliseconds

org.openqa.selenium.WebDriverException: 元素在点 (36, 72) 处不可点击。其他元素将收到点击:...命令持续时间或超时:393 毫秒

If I use Thread.sleep(2000)I don't receive any warnings.

如果我使用,Thread.sleep(2000)我不会收到任何警告。

@Test(dataProvider = "menuData")
public void Main(String btnMenu, String TitleResultPage, String Text) throws InterruptedException {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    driver.findElement(By.id("navigationPageButton")).click();

    try {
       wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(btnMenu)));
    } catch (Exception e) {
        System.out.println("Oh");
    }
    driver.findElement(By.cssSelector(btnMenu)).click();
    Assert.assertEquals(driver.findElement(By.cssSelector(TitleResultPage)).getText(), Text);
}

回答by fg78nc

You can try

你可以试试

WebElement navigationPageButton = (new WebDriverWait(driver, 10))
 .until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();

回答by DebanjanB

WebDriverException: Element is not clickable at point (x, y)

WebDriverException:元素在 (x, y) 点不可点击

This is a typical org.openqa.selenium.WebDriverExceptionwhich extends java.lang.RuntimeException.

这是一个典型的org.openqa.selenium.WebDriverException,它扩展了java.lang.RuntimeException

The fields of this exception are :

此异常的字段是:

  • BASE_SUPPORT_URL: protected static final java.lang.String BASE_SUPPORT_URL
  • DRIVER_INFO: public static final java.lang.String DRIVER_INFO
  • SESSION_ID: public static final java.lang.String SESSION_ID


About your individual usecase, the error tells it all :

关于您的个人用例,错误说明了一切:

WebDriverException: Element is not clickable at point (x, y). Other element would receive the click 

It is clear from your code block that you have defined the waitas WebDriverWait wait = new WebDriverWait(driver, 10);but you are calling the click()method on the element before the ExplicitWaitcomes into play as in until(ExpectedConditions.elementToBeClickable).

它是从你的代码块清楚,你所定义的wait作为WebDriverWait wait = new WebDriverWait(driver, 10);,但您呼叫的click()元素上的方法之前ExplicitWait进场中until(ExpectedConditions.elementToBeClickable)

Solution

解决方案

The error Element is not clickable at point (x, y)can arise from different factors. You can address them by either of the following procedures:

错误Element is not clickable at point (x, y)可能由不同的因素引起。您可以通过以下任一程序解决它们:

1. Element not getting clicked due to JavaScript or AJAX calls present

1. 由于存在 JavaScript 或 AJAX 调用,元素没有被点击

Try to use ActionsClass:

尝试使用Actions类:

WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();

2. Element not getting clicked as it is not within Viewport

2. 元素没有被点击,因为它不在视口

Try to use JavascriptExecutorto bring the element within the Viewport:

尝试使用JavascriptExecutor将元素带入视口:

WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement); 

3. The page is getting refreshed before the element gets clickable.

3. 页面在元素可点击之前刷新。

In this case induce ExplicitWaiti.e WebDriverWaitas mentioned in point 4.

在这种情况下,如第 4 点所述,诱导ExplicitWaitWebDriverWait

4. Element is present in the DOM but not clickable.

4. 元素存在于 DOM 中但不可点击。

In this case induce ExplicitWaitwith ExpectedConditionsset to elementToBeClickablefor the element to be clickable:

在这种情况下, 将ExplicitWaitExpectedConditions设置elementToBeClickable为可点击元素:

WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));

5. Element is present but having temporary Overlay.

5. 元素存在但具有临时覆盖。

In this case, induce ExplicitWaitwith ExpectedConditionsset to invisibilityOfElementLocatedfor the Overlay to be invisible.

在这种情况下,诱导ExplicitWaitExpectedConditions设置为invisibilityOfElementLocated用于叠加到不可见。

WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));

6. Element is present but having permanent Overlay.

6. 元素存在但具有永久覆盖。

Use JavascriptExecutorto send the click directly on the element.

用于JavascriptExecutor直接在元素上发送点击。

WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);

回答by Rester Test

In case you need to use it with Javascript

如果您需要在 Javascript 中使用它

We can use arguments[0].click() to simulate click operation.

我们可以使用arguments[0].click()来模拟点击操作。

var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);

回答by rescdsk

I ran into this error while trying to click some element (or its overlay, I didn't care), and the other answers didn't work for me. I fixed it by using the elementFromPointDOM API to find the element that Selenium wanted me to click on instead:

我在尝试单击某个元素(或其覆盖层,我不在乎)时遇到了这个错误,其他答案对我不起作用。我通过使用elementFromPointDOM API 来查找 Selenium 希望我点击的元素来修复它:

element_i_care_about = something()
loc = element_i_care_about.location
element_to_click = driver.execute_script(
    "return document.elementFromPoint(arguments[0], arguments[1]);",
    loc['x'],
    loc['y'])
element_to_click.click()

I've also had situations where an element was moving, for example because an element above it on the page was doing an animated expand or collapse. In that case, this Expected Condition class helped. You give it the elements that are animated, not the ones you want to click. This version only works for jQuery animations.

我也遇到过元素移动的情况,例如因为页面上元素上方的元素正在执行动画展开或折叠。在这种情况下,这个预期条件类有所帮助。你给它动画元素,而不是你想要点击的元素。此版本仅适用于 jQuery 动画。

class elements_not_to_be_animated(object):
    def __init__(self, locator):
        self.locator = locator

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            # :animated is an artificial jQuery selector for things that are
            # currently animated by jQuery.
            return driver.execute_script(
                'return !jQuery(arguments[0]).filter(":animated").length;',
                elements)
        except StaleElementReferenceException:
            return False

回答by Sudheesh.M.S

Scrolling the page to the near by point mentioned in the exception did the trick for me. Below is code snippet:

将页面滚动到异常中提到的逐点对我来说是诀窍。下面是代码片段:

$wd_host = 'http://localhost:4444/wd/hub';
$capabilities =
    [
        \WebDriverCapabilityType::BROWSER_NAME => 'chrome',
        \WebDriverCapabilityType::PROXY => [
            'proxyType' => 'manual',
            'httpProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
            'sslProxy' => PROXY_DOMAIN.':'.PROXY_PORT,
            'noProxy' =>  PROXY_EXCEPTION // to run locally
        ],
    ];
$webDriver = \RemoteWebDriver::create($wd_host, $capabilities, 250000, 250000);
...........
...........
// Wait for 3 seconds
$webDriver->wait(3);
// Scrolls the page vertically by 70 pixels 
$webDriver->executeScript("window.scrollTo(0, 70);");

NOTE:I use Facebook php webdriver

注意:我使用Facebook php webdriver

回答by user2274204

The best solution is to override the click functionality:

最好的解决方案是覆盖点击功能:

public void _click(WebElement element){
    boolean flag = false;
    while(true) {
        try{
            element.click();
            flag=true;
        }
        catch (Exception e){
            flag = false;
        }
        if(flag)
        {
            try{
                element.click();
            }
            catch (Exception e){
                System.out.printf("Element: " +element+ " has beed clicked, Selenium exception triggered: " + e.getMessage());
            }
            break;
        }
    }
}

回答by kokabi

In C#, I had problem with checking RadioButton, and this worked for me:

在 C# 中,我在检查时遇到问题RadioButton,这对我有用:

driver.ExecuteJavaScript("arguments[0].checked=true", radio);

回答by Nagarjuna Yalamanchili

Can try with below code

可以试试下面的代码

 WebDriverWait wait = new WebDriverWait(driver, 30);

Pass other element would receive the click:<a class="navbar-brand" href="#"></a>

传递其他元素将收到点击<a class="navbar-brand" href="#"></a>

    boolean invisiable = wait.until(ExpectedConditions
            .invisibilityOfElementLocated(By.xpath("//div[@class='navbar-brand']")));

Pass clickable button id as shown below

传递可点击的按钮 id,如下所示

    if (invisiable) {
        WebElement ele = driver.findElement(By.xpath("//div[@id='button']");
        ele.click();
    }