0

The text "Congratulations!" apprears on the site only when the transaction is successful.

I am trying to capture the text of this element using JavascriptExecutor as the type is set to hidden but selenium always displays:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='form-header']//div[contains(text(),'Congratulations')]"}
  (Session info: chrome=85.0.4183.121)

Code 1 (Not Working)

WebElement ele = driver.findElement(By.xpath(//div[@class='form-header']//div[contains(text(),'Congratulations')]));
JavascriptExecutor js = (JavascriptExecutor)driver;
String text = (String)js.executeScript("return arguments[0].value",ele);
System.out.println(text);

Code 2 (Not Working)

WebElement ele = driver.findElement(By.xpath(//div[@class='form-header']//div[contains(text(),'Congratulations')]));
    JavascriptExecutor js = (JavascriptExecutor)driver;
    String text = (String)js.executeScript("return arguments[0].innerHTML",ele);
System.out.println(text);

The HTML code for this part is as follows:

<input id="hdnWindowLocationHost" name="hdnWindowLocationHost" type="hidden" value="/" data-value="themes/custom">
<div id="sgw" class="sgw SGW-header">
             
        </div>
<div class="page">
        <div class="content">
            <div class="form">
                <div class="form-header">
                    <div class="headline">Congratulations!</div>
                    Your password has been reset. Please log in below with your Username and password.
                </div>
Bimlesh
  • 269
  • 2
  • 9
  • 20
  • Does the transaction currently work? Or is there an iframe on the page? – Arundeep Chohan Sep 24 '20 at 19:52
  • I would try with just "//*[contains(text(),'Congratulations')]" with a webdriverwait. (I'm guessing that this element is loaded via javascript...but could also be a frame as mentioned above...) You can then get the element's text by doing: ele.getAttribute("innerHTML"); – pcalkins Sep 24 '20 at 19:54
  • No luck even after making the changes in the xpath and implementing webdriverwait. WebDriverWait wait = new WebDriverWait(driver, 20); WebElement ele = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(text(),'Congratulations')]"))); String text = ele.getAttribute("innerHTML"); System.out.println(text); – Bimlesh Sep 24 '20 at 21:04
  • org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //*[contains(text(),'Congratulations')] (tried for 20 second(s) with 500 milliseconds interval) – Bimlesh Sep 24 '20 at 21:05
  • AND there is no iframe here. – Bimlesh Sep 24 '20 at 21:05
  • Open the site in Chrome and walk through the steps and once you see the message, try your locator in the devtools console as `$x("//div[@class='form-header']//div[contains(text(),'Congratulations')]")`. Does it find the element? – JeffC Sep 24 '20 at 21:09
  • No, it does not find the element. Upon giving this command - I am only seeing - "checking visibility" on the console. – Bimlesh Sep 24 '20 at 21:28

1 Answers1

0

As the element is a dynamic element so to identify the element you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector:

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("div.form-header > div"))).getText());
    
  • Using xpath and text():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='form-header']/div[text()='Congratulations!']"))).getText());
    
  • Using xpath and contains():

    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='form-header']/div[contains(., 'Congratulations')]"))).getText());
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • WebDriverWait wait = new WebDriverWait(driver, 20); WebElement ele = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='form-header']/div[text()='Congratulations!']"))); String text = ele.getText(); System.out.println(text); org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //div[@class='form-header']/div[text()='Congratulations!'] (tried for 20 second(s) with 500 milliseconds interval) – Bimlesh Sep 24 '20 at 21:26