-1

I am trying to get the email and password for Homepage Front End of the below website. http://phptravels.com/demo/

Values that I need to get.
user@phptravels.com
demouser

CODE: driver.get("http://phptravels.com/demo/");

    WebElement user = driver.findElement(By.xpath("/html/body/section[2]/div/div/div[1]/div/div/div[2]/div[2]/div/div[3]/div[2]/div/strong[1]"));
    System.out.println(user.getText());

It will print only "Email", but I just want the values for email and password i.e. user@phptravels.com and "demouser".

I understand I am not writing the correct xpath, but I am not sure how to write for this one.

Ritesh Gupta
  • 81
  • 2
  • 11

3 Answers3

0

You are trying to select the text from inside the following div tag on the page.

<div class="row"> <strong>Email</strong> user@phptravels.com<br> <strong>Password</strong> demouser </div>

For example, you can use the following code to get the node text and split the string to get an array of words in the text.

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://phptravels.com/demo/");
    String my_text = driver.findElement(By.xpath("html/body/section[2]/div/div/div[1]/div/div/div[2]/div[2]/div/div[3]/div[2]/div")).getText();
    String[] words = my_text.split(" "); 
    String email= words[1];
    String password = words[3];
    System.out.println(my_text);

So you will need to use the xpath to that parent div and then use string manipulation techniques to extract the values

see this: Selenium - Get a text node using xpath and use it as a string in Java

and this: Getting text from a node

For a reference on Java string manipulation, see this: http://toolsqa.com/java/basic-java-programming/string-class/

Alexander Higgins
  • 6,765
  • 1
  • 23
  • 41
  • Okay thanks. Now can you suggest me how to get this only "Email user@phptravels.com" ? I don't want the password. – Ritesh Gupta Jun 26 '17 at 17:18
  • As stated you need to use regular string manipulation methods. I have added sample code to my answer, See this for further reference: http://toolsqa.com/java/basic-java-programming/string-class/ – Alexander Higgins Jun 26 '17 at 17:48
0

Here is the Answer to your Question:

To get the the following text on the console:

Email user@phptravels.com

Password demouser

You can use the following code block:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://phptravels.com/demo/");
    String my_text = driver.findElement(By.xpath("html/body/section[2]/div/div/div[1]/div/div/div[2]/div[2]/div/div[3]/div[2]/div[starts-with(normalize-space(),'Email')]")).getText();
    System.out.println(my_text);

Let me know if this Answers your Question.

Community
  • 1
  • 1
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I just want the email and password values. Email and password should not be present when I fetch the values. I want to store the user@phptravels.com in one string variable and demouser in another. – Ritesh Gupta Jun 26 '17 at 17:00
  • I edited the question for better understanding. I think I was not clear before. Please have a look and let me know if it is possible without trimming. Thanks already. – Ritesh Gupta Jun 26 '17 at 17:13
0

Here is the answer to your question.

Output will be:

user@phptravels.com

demouser

Code:

    driver.get("http://phptravels.com/demo/");
    String my_text=driver.findElement(By.xpath("//div[3]/div[2]/div")).getText();
    System.out.println(my_text.substring(my_text.indexOf("Email")+5,my_text.indexOf("Password")));
 System.out.println(my_text.substring(my_text.indexOf("Password")+8));
Monika
  • 714
  • 1
  • 4
  • 10