【问题标题】:Unable to locate element error in Java-Selenium无法在 Java-Selenium 中找到元素错误
【发布时间】:2021-05-18 08:23:17
【问题描述】:

我已经写了代码:

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("New CDA Request")));
element.click();

运行脚本时出现以下错误:

ChromeDriver was started successfully.
Feb 15, 2021 5:18:36 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.className: New CDA Request (tried for 10 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at Automation.CreateCDARequest.main(CreateCDARequest.java:30)
Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":".New\ CDA\ Request"}
  (Session info: chrome=88.0.4324.150)

在出现错误之前,我会在屏幕中弹出一个允许通知的窗口。弹出窗口是否创建问题?

请提出建议。

【问题讨论】:

  • 在不知道带有类名和 id 的实际 html 的情况下,我们应该如何知道如何从屏幕截图中访问该元素?也许By.className("New CDA Request")) 是正确的,但据我所知,css 类名不能包含空格,所以选择器对我来说看起来很可疑。
  • 路径请参考附图。让我知道使用什么路径。

标签: java selenium selenium-webdriver xpath webdriverwait


【解决方案1】:

新 CDA 请求<button>textContent。要点击WebElement,您可以使用以下任一Locator Strategies

  • xpath:

    driver.findElement(By.xpath("//button[contains(@class, 'slds-button') and text()='New CDA Request']")).click();
    

理想情况下,在需要为elementToBeClickable() 诱导WebDriverWait 的元素上使用click(),您可以使用以下任一Locator Strategies

  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(@class, 'slds-button') and text()='New CDA Request']"))).click();
    

通知弹出窗口

您可以在以下位置找到一些有关处理通知弹出窗口的相关讨论:

【讨论】:

  • @Shalu 很高兴能为您提供帮助。请accept answer 点击我的answer 旁边的空心刻度线,该刻度线就在 votedown 箭头下方,这样刻度线就会转动绿色.
猜你喜欢
  • 1970-01-01
  • 2019-07-21
  • 2013-09-16
  • 2017-12-25
  • 2015-03-06
  • 2018-08-01
  • 1970-01-01
  • 2018-01-10
相关资源
最近更新 更多