调试"元素在点上无法点击"错误

use*_*169 371 selenium automated-tests google-chrome selenium-webdriver

我只在Chrome中看到这一点.

完整的错误消息显示:

"org.openqa.selenium.WebDriverException:元素在点(411,675)处不可点击.其他元素将收到点击:..."

"将接收点击"的元素位于相关元素的一侧,而不是位于其上方且不与其重叠,而不是在页面上移动.

我试过添加一个偏移量,但这也不起作用.该项目位于显示的窗口上,无需滚动.

小智 308

这是由以下3种类型引起的:

1.单击时无法看到该元素.

使用ActionsJavascriptExecutor使其单击.

按行动:

WebElement element = driver.findElement(By("element_path"));

Actions actions = new Actions(driver);

actions.moveToElement(element).click().perform();
Run Code Online (Sandbox Code Playgroud)

通过JavascriptExecutor:

JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript("scroll(250, 0)"); // if the element is on top.

jse.executeScript("scroll(0, 250)"); // if the element is on bottom.
Run Code Online (Sandbox Code Playgroud)

要么

JavascriptExecutor jse = (JavascriptExecutor)driver;

jse.executeScript("arguments[0].scrollIntoView()", Webelement); 
Run Code Online (Sandbox Code Playgroud)

然后单击元素.

2.页面在单击元素之前会刷新.

为此,使页面等待几秒钟.

3.元素是可点击的,但顶部有一个微调/覆盖

以下代码将一直等到叠加层消失

By loadingImage = By.id("loading image ID");

WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);

wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));
Run Code Online (Sandbox Code Playgroud)

然后单击元素.

  • 还有第三种原因,即您的元素包含在div或span中.页面可以完全加载,完全在视口内,但Chromedriver将拒绝单击它,FF和IE的webdriver没有问题.真正的人类不知道跨度存在,并且浏览器在您实际点击它时不关心,作为人类.无论是四处走动还是等待都无法解决这个问题; 要么避免Chrome/chromedriver,要么重写页面的HTML似乎是案例3中人们的唯一选择. (48认同)
  • @DonSimon我有同样的问题.我能够通过使用.SendKeys(Keys.Return)而不是.Click来绕过它.这对我有用,我在Chrome中有这个问题,在Firefox中我在这个特定的链接上有另一个类似的问题(锚包裹在Div中). (48认同)
  • @PeterBernier建议的解决方案适用于元素包含在div或span中的情况.在Python中,我使用.send_keys('\n')来模拟点击并解决Chrome中的问题. (15认同)
  • 如果元素被div元素隐藏,则以下代码修复它.executeJavascript("arguments [0] .click();",selenium.findElement(locator)); (3认同)

Bar*_*ala 62

您也可以使用JavaScript点击和滚动.

IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);
Run Code Online (Sandbox Code Playgroud)

  • 难以置信的!我想知道为什么selenium不会破坏它们提供的click()方法,因为有些条件是isDisplayed()和isEnabled()不足以点击一个元素,这似乎是唯一的解决方案.谢谢! (5认同)
  • +1:这是对我有用的解决方案。“ actions.MoveToElement”在C#中对我不起作用,其他滚动解决方案似乎有些脆弱,因为滚动可能无法正确显示元素,或者元素在页面上的位置可能会改变。 (2认同)
  • 如果元素不在视图中,但存在于页面上,则此解决方案是最简单的 (2认同)

Ton*_*ada 45

chromedriver中似乎有一个错误(问题是它被标记为无法修复) - > GitHub Link

(也许会给自由赞助商带来赏金?)

评论#27中提出了一种解决方法.也许它对你有用.

  • 看似合理 - 如果按钮不在屏幕上,那么它实际上不是可点击的. (4认同)

小智 34

我有同样的问题,尝试了所有提供的解决方案,但他们不适合我.最后我用过这个:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("var evt = document.createEvent('MouseEvents');" + "evt.initMouseEvent('click',true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0,null);" + "arguments[0].dispatchEvent(evt);", findElement(element));
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助


opi*_*als 17

在硒驱动的Chrome窗口打开太小的情况下,我已经看到过这种情况.要单击的元素超出视口,因此失败.

这听起来合乎逻辑......真正的用户必须调整窗口大小或滚动,以便可以看到元素并实际上点击它.

在指示硒司机适当设置窗口大小后,这个问题就消失了.该API的webdriver被描述下在这里.


Xwr*_*eia 15

哇,这里有很多答案,还有很多好的答案.

我希望我能从我的经验中加入一些东西.

好吧,在我的情况下,偶尔有一个cookie叠加隐藏元素.滚动到元素也有效; 但是以我的拙见(对我的情况来说,并不是每个人的灵丹妙药)最简单的解决方案就是全屏(我在屏幕窗口的3/4上运行我的脚本)!所以我们走了:

driver.manage().window().maximize();
Run Code Online (Sandbox Code Playgroud)

希望有所帮助!


Shu*_*ain 14

您需要在该元素上使用焦点或滚动。您可能还必须使用显式等待。

WebElement firstbutton= driver.findElement(By.xpath("Your Element"));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
Run Code Online (Sandbox Code Playgroud)

或者

该元素不可点击,因为它上面有一个 Spinner/Overlay:

By loadingImage = By.id("loading image ID");
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(ExpectedConditions.invisibilityOfElementLocated(loadingImage));
Run Code Online (Sandbox Code Playgroud)

或者

Point p= element.getLocation();
Actions actions = new Actions(driver);
actions.moveToElement(element).movebyoffset(p.x,p.y).click().perform();
Run Code Online (Sandbox Code Playgroud)

或者

如果还是不行就用 JavascriptExecutor

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", firstbutton);
Run Code Online (Sandbox Code Playgroud)


Ann*_*net 13

红宝石/的Watir-的webdriver /镀铬

我使用以下技巧,似乎它的工作原理:

#scroll to myelement
@browser.execute_script "window.scrollTo(#{myelement.element.wd.location[0]},#{myelement.element.wd.location[1]})"

# click myelement
myelement.when_present.fire_event("click")
Run Code Online (Sandbox Code Playgroud)


小智 13

我也和这个问题搏斗.代码在FF中工作正常,在Chrome上失败.我试图做的是点击一个复选框 - 如果它不在视图中,我滚动查看然后单击.即使滚动到视图在Chrome中工作,只有勾选框的底部几个像素不可见,因此webdriver拒绝单击它.

我的解决方法是:

WebElement element = _sectorPopup.findElement(...);

((Locatable) element).getCoordinates().inViewPort();
try {
    element.click();
} catch (Exception e) {
    new Actions(getWebDriver()).sendKeys(Keys.PAGE_DOWN).perform();
    element.click();
}
Run Code Online (Sandbox Code Playgroud)

Chrome也存在sendKeys的问题,有时需要使用Actions.显然,你需要知道你需要去哪个方向和多少,所以你的里程可能会有所不同.但我更喜欢这个javascript hack,所以我在这里发布它,以防其他人发现它有用.


u-p*_*ria 12

使用xvfb-run运行无头测试时出现此错误.他们在当地完美无瑕地工作.使用chrome,webdriver/chromedriver/chrome/java等版本都相同.

chromedriver中的"不会修复"错误 - 由TonyLâmpada指出的GitHub Link建议这可能与屏幕上显示的内容有关.

xvfb-run的帮助消息显示以下内容:

-s ARGS   --server-args=ARGS    arguments (other than server number and
                                "-nolisten tcp") to pass to the Xvfb server
                                (default: "-screen 0 640x480x8")
Run Code Online (Sandbox Code Playgroud)

更改xvfb的分辨率使错误消失:

xvfb-run -s "-screen 0 1280x1024x16" ...
Run Code Online (Sandbox Code Playgroud)


Gal*_*cha 9

使用量角器时,这有助于我:

var elm = element(by.css('.your-css-class'));
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());
elm.click();
Run Code Online (Sandbox Code Playgroud)


Suj*_*ewa 8

首先,尝试获取最新的Chrome驱动程序并检查它是否解决了问题.

就我而言,它没有解决问题.但是,到目前为止,以下解决方案对我有用.以下是C#代码,但您可以按照特定语言使用相同的逻辑.我们在这里做的是,

第1步:使用Selenium Actions对象关注元素,

第2步:然后单击元素

第3步:如果有异常,那么我们通过Selenium浏览器驱动程序的"ExecuteScript"方法执行javascript脚本,在元素上触发javascript"Click"事件.

您也可以跳过步骤1和2,也只尝试步骤3.第3步可以自己动手但我注意到在一个场景中有一些奇怪的行为,即使它成功点击了元素,第3步在单击元素后导致代码的其他部分出现意外行为.

            try
            {
                //Setup the driver and navigate to the web page...
                var driver = new ChromeDriver("folder path to the Chrome driver");
                driver.Navigate().GoToUrl("UrlToThePage");

                //Find the element...
                var element = driver.FindElement(By.Id("elementHtmlId")); 

                //Step 1
                new Actions(driver).MoveToElement(element).Perform();  

                //Step 2
                element.Click();
            }
            catch (Exception)
            {
                //Step 3
                driver.ExecuteScript("document.getElementById('elementHtmlId').click();");

            }
Run Code Online (Sandbox Code Playgroud)


Rog*_*rza 7

我根据TonyLâmpada的回答发表了这个方法.它工作得很好.

def scroll_to(element)
  page.execute_script("window.scrollTo(#{element.native.location.x}, #{element.native.location.y})")
end
Run Code Online (Sandbox Code Playgroud)


psr*_*psr 6

我在python中运行selenium脚本时遇到了同样的问题.这是我以前点击元素:

from selenium.webdriver.common.action_chains import ActionChains


ActionChains(driver).click(element).perform()
Run Code Online (Sandbox Code Playgroud)


Mil*_*ind 5

我面临一个类似的问题,我必须一个接一个地检查两个复选框.但我得到了相同的上述错误.因此我在我的步骤之间添加等待检查复选框....它的工作正常和伟大.以下是步骤: -

  When I visit /administrator/user_profiles
  And I press xpath link "//*[@id='1']"
  Then I should see "Please wait for a moment..."
  When I wait for 5 seconds
  And I press xpath link "//*[@id='2']"
  Then I should see "Please wait for a moment..."
  When I visit /administrator/user_profiles_updates
Run Code Online (Sandbox Code Playgroud)


Sin*_*i P 5

今天我遇到了同样的问题。如果我说我是如何解决这个问题的,你不会相信我。

By maximizing the browser size

是的,这是一个指针问题,意味着浏览器的大小。为此,您只需手动或通过代码最大化窗口大小。