简体   繁体   中英

How to access text in a website using Java Selenium

How to access text in a website using Java Selenium?

HTML snapshot:

在此处输入图像描述

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

I suppose you should be able to get these values from those elements values.
Ie 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

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]")))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM