简体   繁体   中英

How to get the value of next element in java selenium

I have total 3 pages and all three pages contains the email ids and I want that email id using java selenium. But all three pages contains different format of email field and I want that in one single code because I am using the for loop to get that email ids.

Page 1:

<p>Test1</p>
<p>abc@abc.com</p>

Page 2:

<p>
   <br>Contact:<br>  
   Test2
</p>
<p>
   <br>Email:<br>  
   xyz@xyz.com
</p>

Page 3:

<p>
   <br>Contact:<br>  
   Test3
</p>
<p>
   <br>Email:<br>  
   pqr@pqr.com
</p>

So how will I get this email ids in one single code.

I tried

email = driver.findElement(By.xpath("//*[contains(text(), 'Email:')]")).getText();

but I got the "email" in that variable but didn't get its value.

This should get whole content of P tag which contains email

WebElement emailElement = driver.findElement(By.xpath("//p[contains(.,'@')]"));
String email = emailElement.getText().toString();

After getting text as string you can split or use a stringbuilder to construct actual email.

hope this helps

试试这个代码:

email = driver.findElement(By.xpath("//*[contains(text(), 'Email:')]/..")).getText();

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