如何自动化 Google Home Page 自动建议? [英] How to automate Google Home Page auto suggestion?
本文介绍了如何自动化 Google Home Page 自动建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的测试类,包含所有代码行.我认为问题出在 xpath 中,因为它无法找到元素.
打包实践;导入 java.util.List;导入 org.openqa.selenium.By;导入 org.openqa.selenium.Keys;导入 org.openqa.selenium.WebDriver;导入 org.openqa.selenium.WebElement;导入 org.openqa.selenium.chrome.ChromeDriver;公共类 selPractice {public static void main(String[] args) 抛出 InterruptedException{String key="webdriver.chrome.driver";字符串值="./software/chromedriver.exe";System.setProperty(key, value);WebDriver 驱动程序=新的 ChromeDriver();driver.get("https://www.google.com");//自动建议driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("motogp");Listmotolist=driver.findElements(By.xpath("//ul[@role='listbox]//li/descendant::div[@class='sbl1']"));线程睡眠(2000);int count=motolist.size();System.out.println(count);for(WebElement 列表:motolist){字符串文本=list.getText();System.out.println(text);}}}
解决方案
从
在这里您可以找到有关 Python Selenium 测试.如何从 Google 首页的搜索框中提取自动建议?
This is my test class with all the lines of code. I think the issue is in the xpath because of that it is not able to find the elements.
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);
}
}
}
解决方案
To extract the Auto Suggestions from the Search Box on Google Home Page you have to induce WebDriverWait for the visibilityOfAllElements and you can use the following solution:
Code Block:
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); } } }
Console Output:
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
Browser Snapshot:
Here you can find a relevant discussion on Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page?
这篇关于如何自动化 Google Home Page 自动建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文