-4

I am trying to finish a test script written in Java. Everything works great and I am trying to print out a confirmation if all previous steps runs which says that account creation was successful.

To do this, I have put the element in a string and then print out the element using the System.out.println command. But for some reason , all I am getting is the following

CONFIRMATION:
That's it. It's supposed to show the string text which reads " Customer information added successfully" . I been trying for the past 2 hours to get it to print it. Can someone please help ?

Here's my code that's causing the problem.

String conf = driver.findElement(By.id("MainContent_lblTransactionResult")).getText();
    System.out.println("CONFIRMATION: " + conf);

I have checked and rechecked the element id and it is the correct element id.

https://i.stack.imgur.com/pAmxH.jpg ( Confirmation page )

https://i.stack.imgur.com/Rt4t2.jpg ( Eclipse output )

JMathew
  • 11
  • 1
  • 5
  • Do you get a valid return from driver.findElement(By.id(...))? – Zachary Dec 22 '17 at 01:15
  • How do you mean? I am trying to get a valid return by printing out the string named "conf". Is that what you meant? Sorry. – JMathew Dec 22 '17 at 01:19
  • WebElement we = driver.findElement(By.id("MainContent_lblTransactionResult")); Check if a WebElement is returned – Zachary Dec 22 '17 at 01:22
  • I tried to run it, but it's giving me a red x mark next to the line.Upon hovering on WebElement, it's saying " Note: This element neither has attached source nor attached Javadoc and hence no Javadoc could be found. " – JMathew Dec 22 '17 at 01:28
  • what is the error message return? – TuyenNTA Dec 22 '17 at 01:31
  • When I click on the red x mark next to the WebElement line, it says "Syntax error on tokens, delete these tokens" – JMathew Dec 22 '17 at 01:33
  • There is a weird token between the last two parenthesis. Remove that. – Zachary Dec 22 '17 at 01:36
  • How do I remove that? I am fairly new to Selenium. sorry. – JMathew Dec 22 '17 at 01:38
  • The actual line I provided has an invalid token in it somehow. Retype the sentence or remove the token. – Zachary Dec 22 '17 at 01:45
  • I just retyped the sentence and the x mark went away. But it's still not printing anything for the webelement "we". I did a sys out and it appears that it's skipping the line. System.out.println("test" + we); – JMathew Dec 22 '17 at 01:56
  • Nevermind my last comment. I saved my file as UTF-8 and now it's printing this following :- test[[FirefoxDriver: firefox on XP (0acb075a-3a0b-443e-940e-80a62a3a575a)] -> id: MainContent_lblTransactionResult] – JMathew Dec 22 '17 at 02:02
  • Is your problem resolved changing to UTF-8? – Zachary Dec 22 '17 at 02:29
  • Thanks Zachary for all your help! I figured it out. For some weird reason, my computer was executing the lines super fast . So fast for it to catch up. So I added a new line that forced it to wait until and only until it actually sees the element - WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​id("ID"))); Now it works! – JMathew Dec 22 '17 at 02:36
  • 1
    Please read why [a screenshot of code is a bad idea](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors). Paste the code and properly format it instead. – JeffC Dec 22 '17 at 05:06
  • Hi Jeff, the reason why I screenshot my code is because of the character limit . Please don't negative vote my post unnecessarily next time. – JMathew Dec 22 '17 at 15:54

2 Answers2

0

Try this

String conf = driver.findElement(By.id("MainContent_lblTransactionResult")).innerHTML;

What you are trying to pull out of the element is in the innerHTML

Ali Azam
  • 2,047
  • 1
  • 16
  • 25
  • I am trying to get the text message. That's why the code has getText(); in the end. It's just a confirmation message. So my last line is System.out.println("CONFIRMATION: " + conf); But the conf string is not getting picked up. – JMathew Dec 22 '17 at 01:35
  • Here it is :- Customer information added successfully – JMathew Dec 22 '17 at 01:51
  • I assume you are reading the page after it is updated. Do you add the span to the page or is the span empty and you add the confirmation. – Oakland Ne Dec 22 '17 at 02:14
  • I figured it out. For some weird reason, my computer was executing the lines super fast . So fast for it to catch up. So I added a new line that forced it to wait until and only until it actually sees the element - WebDriverWait wait = new WebDriverWait(driver, 10); WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ID"))); Now it works! – JMathew Dec 22 '17 at 02:35
  • So still curious. Did you add the span or was it pulling the blank prior to the update – Oakland Ne Dec 22 '17 at 02:43
  • I am sorry. I don't understand . Maybe it's because I am new to selenium. But I don't add the span anywhere. The span is in the html code itself. It's just a html tag. Did I answer it correctly? Is this what you were hoping for? Hope that makes sense. – JMathew Dec 22 '17 at 03:19
  • When the page first appears, before the user hits submit, is the span element on the web page and the inner.html is blank – Oakland Ne Dec 22 '17 at 03:27
  • Before the user hits submit , the page has this - . After the user hits submit, the page shows Customer information added successfully . Is this a problem ? – JMathew Dec 22 '17 at 03:37
0

Use some delay like Webdriver wait.until or Thread sleep after hitting the submit.

Try to get text using JavascriptExecutor like below:

JavascriptExecutor jse = (JavascriptExecutor)driver;
String conf = jse.executeScript("$('#MainContent_lblTransactionResult').text();", "");

System.out.println("CONFIRMATION: " + conf);

You can also try using .html() instead of .text()

String conf = jse.executeScript("$('#MainContent_lblTransactionResult').html();", "");

Hopefully it resolves your issue.

Ali Azam
  • 2,047
  • 1
  • 16
  • 25