Shahid Islam
Shahid Islam

Reputation: 15

How to retrieve Yahoo search auto suggestions through selenium and xpath

The following code is generating error in Xpath. The xpath is for dynamic element and I used start-with. I do not get any error before I run the code, but after I run it, eclipse is generating error. Please help:

package com.TSOne.tcone;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class YahooTextSearch {
       public static void main(String[] args) {
         System.setProperty("webdriver.chrome.driver", "/Users/owner/desktop/chromedriver");
         WebDriver driver = new ChromeDriver();
         driver.get("http://www.yahoo.com");
         driver.findElement(By.id("uh-search-box")).sendKeys("selen");
         List<WebElement> list=driver.findElements(By.xpath("//*[starts-with(@id,'yui_3_18_0_3_1528696’)]"));
         System.out.println(list.size());
       }
}

Upvotes: 0

Views: 715

Answers (5)

vinayak D
vinayak D

Reputation: 11

public static void main(String[] args) throws InterruptedException {
    //setting gecko driver
    System.setProperty("webdriver.gecko.driver",paste path of the gecko driver");
    System.setProperty("webdriver.firefox.marionette",paste path of the gecko driver");

    WebDriver driver=new FirefoxDriver();
    //launching yahoo 
    driver.get("https://in.yahoo.com/?p=us");
    //Entering text in to the yahoo search text field
    driver.findElement(By.xpath(".//*[@id='uh-search-box']")).sendKeys("selenium");
    Thread.sleep(5000);

    List<WebElement> ele = driver.findElements(By.xpath(".//ul[@class='yui3-aclist-list']/li/span/span"));

    //getting size of the search results from suggestion box
    System.out.println(ele.size());

    //getting text from the suggestion box
    for (WebElement e : ele) {
        System.out.println(e.getText());
    }
}

try this hope this will work

Upvotes: 0

dangi13
dangi13

Reputation: 1275

You can use this code for doing so :

    WebDriver driver = PlayField.getChromeDriver();  
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.navigate().to("https://www.yahoo.com/");
    driver.manage().window().maximize();

    WebElement inputField = driver.findElement(By.id("uh-search-box"));
    inputField.sendKeys("Bye");
    List<String> autoSuggestions = driver.findElements(By.cssSelector("ul[role='listbox'] li span span"))
                                         .stream()
                                         .map(suggestion -> suggestion.getAttribute("innerText"))
                                         .collect(Collectors.toList());
    System.out.println(autoSuggestions);

If not using implicit wait than also you can wait for visibility of elements for selector "ul[role='listbox'] li span span" .

You can use this xpath also if you only want to do it using xpath: "//ul[@role='listbox']/li/span/span"

Hope it will work for you.

Upvotes: 0

kjhughes
kjhughes

Reputation: 111706

At the very least, change

//*[starts-with(@id,'yui_3_18_0_3_1528696’)]
                                         ^

to

//*[starts-with(@id,'yui_3_18_0_3_1528696')]
                                         ^

(Replace the marked curly single quote with a straight single quote.)

Follow-up with details if you have additional problems. Note that //* is a costly operation in general in XPath, and particularly in Selenium [thanks for helpful comment, @cruisepandey] – specifying an element name, if possible, would avoid potential performance issue.

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193268

To retrieve the Yahoo search auto suggestions you can use the following solution:

  • Code Block:

    System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.yahoo.com");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#uh-search-box"))).sendKeys("selen");
    List<WebElement> yahoo_search_auto_suggestions = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul.yui3-aclist-list li>span")));
    for (WebElement suggestion:yahoo_search_auto_suggestions)
        System.out.println(suggestion.getText());
    
  • Console Output:

    selena gomez
    selenium
    selena break up
    selena gomez pics
    selena gomez songs
    selenium ide
    selenium tutorial
    selena quintanilla
    seleniumhq.org
    selenium interview questions
    

Browser Snapshot:

Yahoo_auto_suggestion

Upvotes: 0

Monika
Monika

Reputation: 732

If you want to get the size of the list generated after entering the text "Selen" in the search box then you should use the code mentioned below:

      driver.get("http://www.yahoo.com");
      driver.findElement(By.id("uh-search-box")).sendKeys("selen");
      WebDriverWait wait = new WebDriverWait(driver, 60);
      List<WebElement> list=wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//div[@class='yui3-aclist-content']//li[contains(@class,'aclist-item')]")));

      System.out.println(list.size());

Please note that you cannot use the locator "//*[starts- with(@id,'yui_3_18_0_3_1528696')]" because the numbers after yui_3_18_0_3_1528 are changing on reloading the page.

Upvotes: 0

Related Questions