2013-08-22 105 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); 
} 
相關問題