2013-08-22 9 views
9

私はテキスト「Cheese!」を検索しています。 googleのホームページと私は検索ボタンを押した後に検索されたリンクをクリックすることができますかわからない。たとえば、検索ページの上から3番目のリンクをクリックすると、そのリンクを特定してクリックする方法を見つけることができます。これまでの私のコード:Selenium webdriver click google search

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

Googleは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

Googleウェブの迅速検査に基づいて、これはあなたが

String path = "ol[id='rso'] h3[class='r'] a"; 
driver.findElements(By.cssSelector(path)).get(2).click(); 
ような何かをしなければならないページリスト内のリンク

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

へのCSSのパスになります

しかし、xpathを使用することもできます。これはベストプラクティスとJQueryロケータとしてはお勧めできませんが、使用できるかどうかはわかりませんそれらを除く他のものArquillian Graphene

+0

これはエラーです。 "メソッドのcss(String)が型の定義されていません" – min2bro

+0

申し訳ありません、それは 'cssSelector'です。回答を編集しました –

2

要素を検索する方法は複数あります(3番目の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の検索よりも簡単なものにセレンを学んでみてください結果ページ。

例 - プレースホルダにテキスト入力フィールドと対話するhttp://www.google.com/intl/gu/contact/

「どのように我々は助けることができるここますか?。」このようにすることができます -

0

シンプルなXPathは次のとおりです。 のXpath = //スパン[テキスト()= 'Google検索']

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

ようこそスタックオーバーフロー。手元に問題を解決するためにコードがどのように役立つのか、それが以前掲示された回答にどのように追加されているのかを説明する説明文を回答に追加する必要があります。 – 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); 
}