如何通过selenium和xpath检索Yahoo搜索自动建议

时间:2018-06-11 23:13:37

标签: java selenium selenium-webdriver xpath webdriver

以下代码在Xpath中生成错误。 xpath用于动态元素,我使用start-with。在运行代码之前我没有收到任何错误,但是在运行它之后,eclipse会产生错误。请帮忙:

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

5 个答案:

答案 0 :(得分:2)

至少,改变

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

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

(用单引号替换标记的单个引用。)

如果您有其他问题,请详细说明。请注意,//*在XPath中通常是一项代价高昂的操作,特别是在Selenium中 [感谢您提供有用的评论,@cruisepandey] - 如果可能,指定元素名称可以避免潜在的性能问题。

答案 1 :(得分:0)

如果您想在搜索框中输入“Selen”文本后获得列表的大小,那么您应该使用下面提到的代码:

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

请注意,您无法使用定位器“ // * [开始 - with(@ id,'yui_3_18_0_3_1528696')] “因为 yui_3_18_0_3_1528 之后的数字在重新加载页面时正在改变。

答案 2 :(得分:0)

要检索 Yahoo搜索 自动建议,您可以使用以下解决方案:

  • 代码块:

    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());
    
  • 控制台输出:

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

浏览器快照:

Yahoo_auto_suggestion

答案 3 :(得分:0)

您可以使用此代码执行此操作:

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

如果不使用隐式等待,也可以等待选择器元素的可见性&#34; ul [role =&#39; listbox&#39;] li span span&#34;

如果您只想使用xpath执行此操作,也可以使用此xpath:&#34; // ul [@role =&#39; listbox&#39;] / li / span / span&#34;

希望它对你有用。

答案 4 :(得分:0)

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

尝试这种希望能奏效的