2013-08-22 109 views
9

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

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



} 

回答

18

谷歌缩小他们的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<WebElement> 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")); 
    } 
} 

这将打印您:

+3

+1这是最简单的,IMO ,最好的解决方案 –

1

基于谷歌网络的快速检查,这将是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定位器,但我不确定是否可以使用他们aynywhere除了在Arquillian Graphene

+0

这是我得到的错误“方法css(字符串)是未定义的类型” – min2bro

+0

对不起,它是'cssSelector',我编辑了我的答案 –

2

会有多种方式来找到一个元素(在你的情况下,第三个谷歌搜索结果)。

的方式之一,将使用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 

为了更好地了解他们每个人,你应该尝试在比谷歌搜索更简单的东西学习硒结果页面。

示例 - 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!"); 
0

简单的XPath定位谷歌搜索框是: 的Xpath = //跨度[文本()= '谷歌搜索']

0
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); 
    } 
} 
+0

欢迎来到Stack Overflow。你应该在你的答案中加上解释,说明你的代码如何帮助解决手头的问题,以及它是如何增加以前发布的答案的。 – Jens

1
@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<WebElement> 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); 
} 
相关问题