How to get the text from below html code using selenium java?

143 Views Asked by At
<ul class="ui-autocomplete ui-front ui-menu ui-widget ui-widget-content category-choices" id="ui-id-3" tabindex="0" style="display: none; top: 285.516px; left: 524.5px; width: 300px;">
<li data-custom-text="location__text" class="ui-menu-item" id="ui-id-50" tabindex="-1">Car Seats and Baby Carriers<div> in <span>Baby and Kids</span> </div></li>

I used below code

String options =driver.findElements(By.xpath("//li//div//span"));
System.out.println(options.getText());

Actual Output: Baby and Kids

Expected output: Car Seats and Baby Carriers in Baby and Kids

2

There are 2 best solutions below

0
On

Try using -

String options = driver.findElements(By.xpath("//li//text()"));
System.out.println(options.getText());
0
On

You went too deep into your nested tag also options is one element and not multiple and is of webelement type.

WebElement options = driver.findElement(By.xpath("//ul/li[class='ui-menu-item']"));

Then this

System.out.println(options.getText());