Selection data from Yahoo search bar using csselector fails | Selenium Forum
M
Posted on 13/12/2015
package p111;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Yahoo_c {

public static void main(String[] args) {
// TODO Auto-generated method stub


WebDriver wi= new FirefoxDriver();
wi.get("https://in.yahoo.com/?p=us");
wi.findElement(By.xpath("//*[@id='UHSearchBox']")).sendKeys("pizza");
try{
Thread.sleep(5000);
}catch(Exception e)
{
System.out.println("wait ended");
}
String sl=wi.findElement(By.cssSelector("*[id^='yui_3_12_0_1_14']")).getText();
//
System.out.println(sl);
//public static By cssSelector(java.lang.String selector)



}

}
-------------------------------------------------------------------------------------------------------------
Following is the code.
When i run this, execution goes until "pizza" being entered into yahoo search.Later with no error message in console execution terminates.

Please help resolve this issue.Am trying to select pizza delivery from list [refer attachment]

M
Replied on 14/12/2015

Try this
[code:2i0osyin] package yahoo;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Yahoo {

public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.yahoo.com");
driver.findElement(By.xpath("//*[@id='UHSearchBox']")).sendKeys("Pizza");


List<WebElement> searchList=driver.findElements(By.xpath("//*[starts-with(@id,'yui')]/li/a"));
System.out.println("Total searched items are= "+searchList.size());//will count all the elements searched by pizza
System.out.println(searchList.get(4).getText());//will print 5th item which is pizza delivery
System.out.println("===============================");


//will print all the available items
for(int i=0;i<10;i++){
if(!(searchList.get(i).getText().isEmpty())){
System.out.println(searchList.get(i).getText());
}

}


}

}[/code:2i0osyin]

I think you need to put all the elements in a list and then access the index of it.