2

How to access text in a website using Java Selenium?

HTML snapshot:

enter image description here

Can someone explain to me how to access the demouser and email id using java selenium in the above picture

https://phptravels.com/demo this is the website im working on

2 Answers 2

0

I suppose you should be able to get these values from those elements values.
I.e. you can get the email input value with this:

WebElement email = driver.findElement(By.xpath("//div[.strong[text()='Email']]"));
String innerEmailvalue = email.getAttribute("value"); 

The same for the second element

Sign up to request clarification or add additional context in comments.

5 Comments

i am just getting null for the above recomended solution
OK, but you are correctly getting the email WebElement? It's not a null etc?
phptravels.com/demo this is the website i'm dealing with im supposed to read all the email is and passwords for all types of users
Moment, have you tried to extract the text with email.getText() ?
yes im still getting null value
0

The text demouser is within a Text Node you have to induce WebDriverWait for the visibilityOfElementLocated() and using executeScript() from JavascriptExecutor you can use the following Locator Strategy:

WebElement myElement = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Password']//ancestor::div[1]")));
String myText = (String)((JavaScriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", myElement);
System.out.println(myText);

In a single line:

System.out.println((String)((JavaScriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//strong[text()='Password']//ancestor::div[1]")))));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.