use*_*169 371 selenium automated-tests google-chrome selenium-webdriver
我只在Chrome中看到这一点.
完整的错误消息显示:
"org.openqa.selenium.WebDriverException:元素在点(411,675)处不可点击.其他元素将收到点击:..."
"将接收点击"的元素位于相关元素的一侧,而不是位于其上方且不与其重叠,而不是在页面上移动.
我试过添加一个偏移量,但这也不起作用.该项目位于显示的窗口上,无需滚动.
小智 308
这是由以下3种类型引起的:
1.单击时无法看到该元素.
使用Actions或JavascriptExecutor使其单击.
按行动:
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)
然后单击元素.
Bar*_*ala 62
您也可以使用JavaScript点击和滚动.
IJavaScriptExecutor ex = (IJavaScriptExecutor)Driver;
ex.ExecuteScript("arguments[0].click();", elementToClick);
Run Code Online (Sandbox Code Playgroud)
Ton*_*ada 45
chromedriver中似乎有一个错误(问题是它被标记为无法修复) - > GitHub Link
(也许会给自由赞助商带来赏金?)
评论#27中提出了一种解决方法.也许它对你有用.
小智 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)
希望这可以帮助
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)
使用量角器时,这有助于我:
var elm = element(by.css('.your-css-class'));
browser.executeScript("arguments[0].scrollIntoView();", elm.getWebElement());
elm.click();
Run Code Online (Sandbox Code Playgroud)
首先,尝试获取最新的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)
我根据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)
我在python中运行selenium脚本时遇到了同样的问题.这是我以前点击元素:
from selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).click(element).perform()
Run Code Online (Sandbox Code Playgroud)
我面临一个类似的问题,我必须一个接一个地检查两个复选框.但我得到了相同的上述错误.因此我在我的步骤之间添加等待检查复选框....它的工作正常和伟大.以下是步骤: -
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)
今天我遇到了同样的问题。如果我说我是如何解决这个问题的,你不会相信我。
By maximizing the browser size
是的,这是一个指针问题,意味着浏览器的大小。为此,您只需手动或通过代码最大化窗口大小。