Yahoo search box list values -Xpath & cssSelector | Selenium Forum
M
Posted on 09/02/2016
i'm trying to retrieve xpath for one of the listed values when a word is entered in the Yahoo search textbox. please help me to find the xpath and cssSelector to print first/second element for Firefox Browser

XPath : //*[@id='yui_3_18_0_4_1455073006651_1079']

my code:
WebDriver driver = new FirefoxDriver();
driver.get("https://yahoo.com");
driver.findElement(By.xpath("//*[@id='uh-search-box']")).sendKeys("Hello");

Thread.sleep(3000);

String res= driver.findElement(By.cssSelector("ul[id^='yui_3_18_0']")).getText();
System.out.println(res);

Results:
Printed all the values
hello adele
hello lyrics
hello
hello kitty
hellofresh.com
hello february...etc

Attached screenshot for reference.

<div id="yui_3_18_0_4_1455073006651_246" class="yui3-widget yui3-aclist yui3-widget-positioned" style="width: 450px; left: 0px; top: 40px;" aria-hidden="false">
<div id="yui_3_18_0_4_1455073006651_248" class="yui3-aclist-content">
<ul id="yui_3_18_0_4_1455073006651_263" class="yui3-aclist-list" role="listbox">
<li id="yui_3_18_0_4_1455073006651_1079" class="yui3-aclist-item yui3-aclist-item-hover" role="option" data-text="hello adele">
<span id="uh-assist-link-1" data-ylk="rspns:nav;t1:a1;t2:srch;t3:tray;sec:srch;slk:srchast;elm:itm;elmt:srch;tar:search.yahoo.com;cpos:1;t9:10;itc:0;" data-position="1">
<b class="yui3-highlight">hello</b>
adele
</span>

M
Replied on 09/02/2016

have you the module on dynamic element and xpaths.


M
Replied on 10/02/2016

Yes, i went through the video more than 2-3 times, but my bad i'm not able to resolve the issue. kindly help me,i''m stuck here with my practice


M
Replied on 10/02/2016

i have tried below code , printed only "hello"

WebDriver driver = new FirefoxDriver();
driver.get("https://yahoo.com");

driver.findElement(By.xpath("//*[@id='uh-search-box']")).sendKeys("Hello");

Thread.sleep(3000);
String res = driver.findElement(By.cssSelector("ul[id^='yui_3_18_0'] b")).getText();
System.out.println(res);

result is : hello

how to achieve complete text i.e.. hello adele


M
Replied on 10/02/2016

use below css selector:

String res = driver.findElement(By.cssSelector("ul[id^='yui_3_18_0'] li:nth-child(1)")).getText();


M
Replied on 10/02/2016

it worked:-) Thanks a lot mohit.