Selenium Webdriver 2.25.0 Java Firefox - 无法识别Google搜索结果Webelement

时间:2012-08-13 09:03:26

标签: firefox selenium firebug webdriver

我是Selenium 2.0 Java Webdriver中的菜鸟。试着靠自己学习。但坚持认定一个Webelement。我想点击Google的搜索结果。我正在搜索字符串'Selenium Download'并试图点击第一个结果。它在Firebug中标识如下 -

<a class="l" onmousedown="return rwt(this,'','','','1','AFQjCNF012aUD3cMR2x-qIQl2u6suxLnlw','','0CE0QFjAA',null,event)" href="http://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CE0QFjAA&url=http%3A%2F%2Fseleniumhq.org%2Fdownload%2F&ei=Wb8oULKfHsjsrAf4_IDICQ&usg=AFQjCNF012aUD3cMR2x-qIQl2u6suxLnlw">
<em>Downloads</em>
-
<em>Selenium</em>
</a>

XPath:/ html / body / div [5] / div [2] / div / div [4] / div [2] / div [2] / div [2] / div [2] / div / ol / LI / DIV / H3 / A

CSSPath:html body#gsr.vsh div #main div div#cnt.mdm div.mw div#rcnt div#center_col div#res.med div#search div#ires ol#rso li.g div.vsc h3 .r al

但我未能确定这一点。我尝试过类似下面的内容 -

public static void main (String[] args) {

    WebDriver ffx1 = new FirefoxDriver();
    ffx1.get("http://www.google.com");
    ffx1.manage().window().maximize();
    ffx1.findElement(By.className("xbtn")).click();
    ffx1.findElement(By.name("q")).sendKeys("saikat");
    ffx1.findElement(By.name("btnG")).click();
    ffx1.findElement(By.xpath(My Expression));

}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

我认为有两件事需要清除:

首先 - 你所拥有的定位器可能不正确 谷歌搜索页面上第一个搜索元素的xpath就是 .//*[@ ID = 'RSO'] /锂[1] / DIV / H3 / A

第二 - 单击搜索按钮后,搜索结果会在搜索结果页面上显示几秒钟。因此,必须插入一些等待时间。我在下面的代码中使用了30秒的隐式等待。隐式等待将告诉Webdriver在尝试查找一个或多个元素(如果它们不能立即可用)时,在一定时间内轮询DOM。

现在,如果您尝试使用以下代码,它应该可以正常工作

import java.util.concurrent.TimeUnit;

    public static void main(String[] args) throws InterruptedException {
        WebDriver ffx1 = new FirefoxDriver();
        ffx1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        ffx1.get("http://www.google.com");
        ffx1.manage().window().maximize();
        ffx1.findElement(By.name("q")).sendKeys("saikat");
        ffx1.findElement(By.name("btnG")).click();
        ffx1.findElement(By.xpath(".//*[@id='rso']/li[1]/div/h3/a")).click();

    }
相关问题