我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
我只在Chrome浏览器中看到这个。
完整的错误信息如下:
“org.openqa.selenium。WebDriverException:元素在点(411,675)不可点击。其他元素会收到点击:……”
“将接收点击”的元素位于相关元素的一侧,而不是在元素的顶部,也没有重叠,也没有在页面上移动。
我试过加一个偏移量,但也不行。该项目在显示的窗口上,不需要滚动。
当前回答
首先,尝试使用最新的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();");
}
其他回答
在Visual Studio 2013中,如果你启用BrowserLink -它会在屏幕底部显示一个BrowserLink导航栏。如果你想点击的项目在导航栏后面,它会给出错误。禁用BrowserLink为我解决了这个问题。
我在使用clj-webdriver (Selenium的clojure端口)时也遇到了同样的问题。为了方便起见,我只是将前面的解决方案翻译为clojure。你可以在点击之前调用这个函数来避免这个问题。
(defn scrollTo
"Scrolls to the position of the given css selector if found"
[q]
(if (exists? q)
(let [ loc (location-once-visible q) jscript (str "window.scrollTo(" (:x loc) "," (:y loc) ")") ]
(execute-script jscript))))
我做了一种蛮力点击,它对我有用。
try:
elem.click()
except:
print "failed to click"
size = elem.size
mid_of_y = int(size["height"])/2
stepts_to_do_to_left = int(size["width"])
while stepts_to_do_to_left > 0:
try:
print stepts_to_do_to_left, mid_of_y
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(elem, mid_of_y, stepts_to_do_to_left)
action.click()
action.perform()
print "DONE CLICK"
break
except:
pass
我也遇到过类似的问题,我必须一个接一个地勾选两个复选框。但我得到了相同的上面的错误。因此,我在步骤之间添加了等待,以检查复选框....它工作得很好。以下是步骤:-
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
如果你试图点击一个被禁用的输入或按钮,这种情况也会发生,在这种情况下,元素没有重叠,但它是不可点击的。