提问人:Maria 提问时间:7/5/2017 最后编辑:Bruno BieriMaria 更新时间:8/1/2021 访问量:216736
Selenium Web 驱动程序 & Java.元素在点 (x, y) 处不可单击。其他元素将收到点击
Selenium Web Driver & Java. Element is not clickable at point (x, y). Other element would receive the click
问:
我使用了显式等待,并收到警告:
org.openqa.selenium.WebDriverException: 元素在点 (36, 72) 处不可单击。其他元素将收到 咔嚓咔嚓:...... 命令持续时间或超时:393 毫秒
如果我使用,我不会收到任何警告。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);
}
答:
你可以试试
WebElement navigationPageButton = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("navigationPageButton")));
navigationPageButton.click();
评论
WebElement element = driver.findElement(By.id("navigationPageButton")); Actions actions = new Actions(driver); actions.moveToElement(element).click().perform();
WebDriverException:元素在点 (x, y) 处不可单击
这是一个典型的 org.openqa.selenium.WebDriverException,它扩展了 java.lang.RuntimeException。
此异常的字段为:
- 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
关于您的个人用例,错误说明了一切:
WebDriverException: Element is not clickable at point (x, y). Other element would receive the click
从您的代码块中可以清楚地看出,您已经定义了 as,但是在 中 开始发挥作用之前,您正在对元素调用该方法。wait
WebDriverWait wait = new WebDriverWait(driver, 10);
click()
ExplicitWait
until(ExpectedConditions.elementToBeClickable)
溶液
错误可能由不同的因素引起。您可以通过以下任一过程来解决这些问题:Element is not clickable at point (x, y)
1. 由于存在 JavaScript 或 AJAX 调用,元素未被单击
尝试使用 Class:Actions
WebElement element = driver.findElement(By.id("navigationPageButton"));
Actions actions = new Actions(driver);
actions.moveToElement(element).click().build().perform();
2. 元素未被单击,因为它不在视口内
尝试使用 JavascriptExecutor
将元素引入视口:
WebElement myelement = driver.findElement(By.id("navigationPageButton"));
JavascriptExecutor jse2 = (JavascriptExecutor)driver;
jse2.executeScript("arguments[0].scrollIntoView()", myelement);
3. 在元素可点击之前,页面正在刷新。
在这种情况下,请诱导 ExplicitWait,即 WebDriverWait,如第 4 点所述。
4. 元素存在于 DOM 中,但不可点击。
在本例中,请将 ExplicitWait 设置为,使元素可单击:ExpectedConditions
elementToBeClickable
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.elementToBeClickable(By.id("navigationPageButton")));
5. 元素存在,但具有临时覆盖。
在这种情况下,诱导 ExplicitWait
,并将 ExpectedConditions
设置为 invisibilityOfElementLocated
以使覆盖不可见。
WebDriverWait wait3 = new WebDriverWait(driver, 10);
wait3.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("ele_to_inv")));
6. 元素存在,但具有永久覆盖。
用于直接在元素上发送单击。JavascriptExecutor
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
评论
如果您需要将其与 Javascript 一起使用
我们可以使用 arguments[0].click() 来模拟点击操作。
var element = element(by.linkText('webdriverjs'));
browser.executeScript("arguments[0].click()",element);
评论
我在尝试单击某些元素(或其覆盖层,我不在乎)时遇到了此错误,而其他答案对我不起作用。我通过使用 DOM API 来查找 Selenium 希望我点击的元素来修复它:elementFromPoint
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()
我也遇到过元素移动的情况,例如,因为页面上它上面的元素正在进行动画展开或折叠。在这种情况下,这个 Expected Condition 类很有帮助。你给它提供动画元素,而不是你想点击的元素。此版本仅适用于 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
将页面滚动到异常中提到的附近点对我来说很成功。下面是代码片段:
$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);");
注意:我使用 Facebook php webdriver
最好的解决方案是覆盖点击功能:
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;
}
}
}
在 C# 中,我在检查时遇到了问题,
这对我有用:RadioButton
driver.ExecuteJavaScript("arguments[0].checked=true", radio);
可以尝试使用以下代码
WebDriverWait wait = new WebDriverWait(driver, 30);
传递其他元素将收到点击:<a class="navbar-brand" href="#"></a>
boolean invisiable = wait.until(ExpectedConditions
.invisibilityOfElementLocated(By.xpath("//div[@class='navbar-brand']")));
传递可点击的按钮 ID,如下所示
if (invisiable) {
WebElement ele = driver.findElement(By.xpath("//div[@id='button']");
ele.click();
}
如果元素不可点击并且存在覆盖问题,则使用 arguments[0].click()。
WebElement ele = driver.findElement(By.xpath("//div[@class='input-group-btn']/input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
评论
#navigationPageButton
elementToBeClickable()