Selenium - 元素在点上不可点击

hae*_*ish 13 javascript selenium selenium-chromedriver

我正在使用selenium来测试脚本.我收到以下错误,随机发生此错误.当我跑10次时,我得到了大约两次.所以它不是真正可重复的.有谁知道为什么会这样?我试图点击的元素在浏览器中肯定是可见的并且不会移动,因此不需要调整大小或拖动元素.我正在使用chrome webdriver并且我阅读了其他故障排除策略(调试"元素在点上无法点击"错误)并且它们似乎与我的问题无关.我也等了足够的时间.

UnknownError: unknown error: Element is not clickable at point (167, 403). Other element would receive the click: <div class="leftMasterBackground"></div>
Run Code Online (Sandbox Code Playgroud)

Joh*_*nny 7

为了在点击不同的UI元素时提高稳定性,您可以执行许多步骤:

  • 明确地等待它在DOM中的存在
  • 滚动到元素视图中
  • 检查是否可点击

它有助于稳定吗?

WebDriverWait wait = new WebDriverWait(driver, 3)
JavascriptExecutor js = ((JavascriptExecutor) driver)

//presence in DOM
wait.until(ExpectedConditions.presenceOfElement(By.id("ID")));

//scrolling
WebElement element = driver.findElement(By.id("ID")));  
js.executeScript("arguments[0].scrollIntoView(true);", element);

//clickable
wait.until(ExpectedConditions.elementToBeClickable(By.id("ID")));
Run Code Online (Sandbox Code Playgroud)

此外,如果您决定使用更多自定义操作界面覆盖默认操作界面,则可以使用两种类型的点击(例如):click()这将具有所有这些稳定性步骤,fastClick()并且将是没有任何变化的默认点击.