Selenium webdriver点击谷歌搜索

我正在搜索文本“奶酪!” 在谷歌主页上,不确定如何在按下搜索按钮后点击搜索到的链接。 例如,我想在搜索页面上单击顶部的第三个链接,然后如何找到标识链接并单击它。 我的代码到目前为止:

package mypackage; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.WebDriverWait; public class myclass { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\selenium-java-2.35.0\\chromedriver_win32_2.2\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); //driver.close(); } } 

谷歌缩小了他们的CSS课程等,因此要识别所有内容并不容易。

此外,您还有一个问题,即必须“等待”,直到站点显示结果。 我会这样做:

 public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!\n"); // send also a "\n" element.submit(); // wait until the google page shows the result WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats"))); List findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a")); // this are all the links you like to visit for (WebElement webElement : findElements) { System.out.println(webElement.getAttribute("href")); } } 

这将打印出来:

可以通过多种方式查找元素(在您的情况下是第三个Google搜索结果)。

其中一种方法是使用Xpath

 #For the 3rd Link driver.findElement(By.xpath(".//*[@id='rso']/li[3]/div/h3/a")).click(); #For the 1st Link driver.findElement(By.xpath(".//*[@id='rso']/li[2]/div/h3/a")).click(); #For the 2nd Link driver.findElement(By.xpath(".//*[@id='rso']/li[1]/div/h3/a")).click(); 

其他选择是

 By.ByClassName By.ByCssSelector By.ById By.ByLinkText By.ByName By.ByPartialLinkText By.ByTagName 

为了更好地理解它们中的每一个,您应该尝试在比Google搜索结果页面更简单的事情上学习Selenium。

示例 – http://www.google.com/intl/gu/contact/

使用占位符与文本输入字段进行交互“我们如何帮助?在这里询问。” 你可以这样做 –

 # By.ByClassName driver.findElement(By.ClassName("searchbox")).sendKeys("Hey!"); # By.ByCssSelector driver.findElement(By.CssSelector(".searchbox")).sendKeys("Hey!"); # By.ById driver.findElement(By.Id("query")).sendKeys("Hey!"); # By.ByName driver.findElement(By.Name("query")).sendKeys("Hey!"); # By.ByXpath driver.findElement(By.xpath(".//*[@id='query']")).sendKeys("Hey!"); 

基于对谷歌网站的快速检查,这将是页面列表中链接的CSS路径

ol[id="rso"] h3[class="r"] a

所以你应该做点什么

 String path = "ol[id='rso'] h3[class='r'] a"; driver.findElements(By.cssSelector(path)).get(2).click(); 

但是你也可以使用xpath ,这不是真正推荐的最佳实践和JQuery定位器,但我不确定你是否可以使用它们除了Arquillian Graphene之外的其他地方

 @Test public void google_Search() { WebDriver driver; driver = new FirefoxDriver(); driver.get("http://www.google.com"); driver.manage().window().maximize(); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!\n"); element.submit(); //Wait until the google page shows the result WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats"))); List findElements = driver.findElements(By.xpath("//*[@id='rso']//h3/a")); //Get the url of third link and navigate to it String third_link = findElements.get(2).getAttribute("href"); driver.navigate().to(third_link); } 

用于查找Google搜索框的简单Xpath是:Xpath = // span [text()=’Google Search’]

 public class GoogleSearch { public static void main(String[] args) { WebDriver driver=new FirefoxDriver(); driver.get("http://www.google.com"); driver.findElement(By.xpath("//input[@type='text']")).sendKeys("Cheese"); driver.findElement(By.xpath("//button[@name='btnG']")).click(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); driver.findElement(By.xpath("(//h3[@class='r']/a)[3]")).click(); driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } }