1

I would would like to capture the number show in the alert shown here. I have tried using the xpath to click on it but it does not work. Appreciate any help.

WebElement attribute = driver.findElement(By.xpath("//span[@class='text']//a"));

J.F.
  • 13,927
  • 9
  • 27
  • 65
  • shown where? If it's a popup alert you won't be able to use the method – ACV Nov 24 '20 at 22:20
  • It is shown on the user interface... this the the info displayed in the console when I inspect the alert.. Success "You successfully created the file" 9988987 – Bryan Noyahr Nov 24 '20 at 22:23
  • I don't see any screenshot or any console example – ACV Nov 24 '20 at 22:24
  • If from browser console, check this: https://stackoverflow.com/questions/25431380/capturing-browser-logs-with-selenium-webdriver-using-java – ACV Nov 24 '20 at 22:25
  • I tried to add it a screenshot, but it would not let me add one. First time posting here. No idea why it keeps rejecting the screenshot. – Bryan Noyahr Nov 24 '20 at 22:26

2 Answers2

0

If it is a browser javascript alert, you need to use this:

//Click the link to activate the alert
driver.findElement(By.linkText("See an example alert")).click();

//Wait for the alert to be displayed and store it in a variable
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

//Store the alert text in a variable
String text = alert.getText();

//Press the OK button
alert.accept();
ACV
  • 9,964
  • 5
  • 76
  • 81
  • Thank you very much. I will try this. – Bryan Noyahr Nov 24 '20 at 22:29
  • just to clarify, by alert I understand a popup opened by the browser (the ones which you can usually block) – ACV Nov 24 '20 at 22:30
  • My script creates a file and after it is saved it displays an alert - with the file number ... I was trying to capture this number to be used to continue the script using it. BTW I used your code and I got - org.openqa.selenium.remote.ErrorCodes toStatus 'no such alert' (400 expected) 404 --> incorrect JSON status mapping for 'no such alert' (400 expected). – Bryan Noyahr Nov 24 '20 at 22:45
0

First, click on the alert link. Then try the below code (it will switch to the present alert-box and fetch the text from that).

driver.switchTo().alert().getText();
arpita biswas
  • 144
  • 1
  • 6