简体   繁体   中英

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

<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

Try using -

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

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());

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