java - 如何自动化 Google 主页自动建议?

标签 java selenium selenium-webdriver webdriverwait

这是我的测试类,包含所有代码行。我认为问题出在 xpath 中,因为它无法找到元素。

package practice;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class selPractice {

public static void main(String[] args) throws InterruptedException
{
    String key="webdriver.chrome.driver";
    String value="./software/chromedriver.exe";
    System.setProperty(key, value);
    WebDriver driver=new ChromeDriver();
    driver.get("https://www.google.com");
    //to automate auto suggestion 

driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("motogp");
List<WebElement>motolist=driver.findElements(By.xpath("//ul[@role='listbox]
//li/descendant::div[@class='sbl1']"));
    Thread.sleep(2000);

    int count=motolist.size();
    System.out.println(count);
    for(WebElement list:motolist)
    {
        String text=list.getText();
        System.out.println(text);
    }
}
}

最佳答案

Google Home Page 上的搜索框提取自动建议您必须为 visibilityOfAllElements 引入 WebDriverWait,并且可以使用以下解决方案:

  • 代码块:

    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;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class Google_Auto_Suggestions {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            options.addArguments("disable-infobars"); 
            options.addArguments("--disable-extensions"); 
            WebDriver driver = new ChromeDriver(options);
            driver.get("http://www.google.com");
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("q"))).sendKeys("motogp");
            List<WebElement> motolist = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//form[@action='/search' and @role='search']//ul[@role='listbox']//li//span")));
            for(WebElement list:motolist)
            {
                String text=list.getText();
                System.out.println(text);
            }
        }
    }
    
  • 控制台输出:

    Only local connections are allowed.
    Dec 04, 2018 6:14:51 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Detected dialect: OSS
    motogp
    motogp 2018
    motogp live
    motogp results
    motogp game
    motogp news
    motogp bikes
    motogp race
    motogp wiki
    motogp schedule
    
  • 浏览器快照:

motogp_google_search_results

在这里您可以找到关于 Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page? 的相关讨论

关于java - 如何自动化 Google 主页自动建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53612605/

相关文章:

python - 在循环中第二次调用“driver.get(url)”后,Selenium出现“错误:已超过最大重试次数”,其中“url”的值每次迭代都会更改

java - 在 IntelliJ IDEA 中执行的 Maven Selenium WebDriver 项目中出现编码警告

python - 是否也可以在不运行 X 服务器的情况下运行 Selenium 脚本?

selenium - 如何在页面完全加载之前启动点击

java - 将目录上传到远程服务器

javascript - 如何使用 selenium webdriver 转储整个 dom 结构

java - Eclipse 自动完成参数类型强调

java - Selenium Java - 列表元素包含无序列表中的部分文本

java - 为什么我的联网 Java 代码每隔一轮都会跳过输出?

java - Ruby 路径和带有存档的小程序