-2

I have written code:

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

Getting below error while running the script:

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)

Before the error, I am getting a pop-up in screen to allow notification. Is the pop-up creating issue?

enter image description here

enter image description here

Please suggest.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Shalu
  • 23
  • 1
  • 6
  • 1
    How are we supposed to know how to reach that element from just a screnshot without knowing the actual html with class names and ids? Maybe `By.className("New CDA Request"))` is correct, but as far as I know css class names cannot contain spaces, so that selector looks suspicious to me. – OH GOD SPIDERS Feb 15 '21 at 12:41
  • Please refer attached image for the path. Let me know what path to use. – Shalu Feb 15 '21 at 12:54

1 Answers1

0

New CDA Request is the textContent of the <button>. To click on the WebElement you can use either of the following Locator Strategies:

  • xpath:

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

Ideally to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:

  • xpath:

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

Notification popup

You can find a couple of relevant discussion on handling the Notification popup in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352