简体   繁体   中英

Unable to locate element in Selenium Webdriver

I am trying to locate element which is having html Code:

<td align="center" colspan="2">OTP : 6363 </td>

& html tag starts from td/ I want to get the text over it so, I tried by writing code driver.findelement(By.id("otp")).getText();

Try your tag name. Example :-

driver.findElement(By.tagName("td")).getText();

As said otp is not id. Its text that would display in that td cell.

For example id will display like this

<input **id="search"** class="test" type="text"/>

so you can try with other locators may by using xpath, cssSelector etc..

for example, xpath with taking consideration as OTP text will display, then

driver.findElement(By.xpath("//td[contains(text(), 'OTP :')]")).getText();

You can also use xpath=//td or By.tagName("td") but mostly more number of td elements will be present in page, so it may not fetch you required unique element.

Please provide total DOM HTML code to suggest locators..

Thanks

Try below code to get output as per your requirement :

String text = driver.findElement(By.tagName("td")).getText();
text = text.replaceAll("\\D+","");
System.out.println(text);

In code , First of all getting text of TD tag, Then removing all non-digits and then getting only digit values.

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