我的简单Selenium程序没有运行?

时间:2015-01-24 05:37:22

标签: java selenium

我正在使用Selenium创建一个简单的程序。它运行到Java。让我先澄清一下我Selenium中的初学者。

所以,我在google中建议的简单程序,在这里:

package foo;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GoogleSuggest {
    public static void main(String[] args) throws Exception {
        // The Firefox driver supports javascript 
        WebDriver driver = new FirefoxDriver();

        // Go to the Google Suggest home page
        driver.get("http://www.google.com/webhp?complete=1&hl=en");

        // Enter the query string "Cheese"
        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Cheese");

        // Sleep until the div we want is visible or 5 seconds is over
        long end = System.currentTimeMillis() + 5000;
        while (System.currentTimeMillis() < end) {
            WebElement resultsDiv = driver.findElement(By.className("gssb_e"));

            // If results have been returned, the results are displayed in a drop down.
            if (resultsDiv.isDisplayed()) {
              break;
            }
        }

        // And now list the suggestions
        List<WebElement> allSuggestions = driver.findElements(By.xpath("//td[@class='gssb_a gbqfsf']"));

        for (WebElement suggestion : allSuggestions) {
            System.out.println(suggestion.getText());
        }

        driver.quit();
    }
}

运行此程序后,它会打开一个外部Firefox browser。并在我的控制台程序中输入一些错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"gssb_e"}
Command duration or timeout: 47 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'Rajendra', ip: '180.215.161.202', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.8.0'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=35.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 13e67b23-d0b6-493d-a936-5601591ee96f
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:156)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByClassName(RemoteWebDriver.java:433)
    at org.openqa.selenium.By$ByClassName.findElement(By.java:387)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at foo.GoogleSuggest.main(GoogleSuggest.java:25)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"class name","selector":"gssb_e"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.44.0', revision: '76d78cf', time: '2014-10-23 20:03:00'
System info: host: 'Rajendra', ip: '180.215.161.202', os.name: 'Windows 8.1', os.arch: 'x86', os.version: '6.3', java.version: '1.8.0'
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///C:/Users/raj/AppData/Local/Temp/anonymous1309276722675302219webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9641:26)
    at <anonymous class>.FirefoxDriver.prototype.findElement(file:///C:/Users/raj/AppData/Local/Temp/anonymous1309276722675302219webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:9650:3)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/raj/AppData/Local/Temp/anonymous1309276722675302219webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:16)
    at <anonymous class>.DelayedCommand.prototype.executeInternal_(file:///C:/Users/raj/AppData/Local/Temp/anonymous1309276722675302219webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11640:7)
    at <anonymous class>.DelayedCommand.prototype.execute/<(file:///C:/Users/raj/AppData/Local/Temp/anonymous1309276722675302219webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:11582:5)

帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

请对您的while循环代码进行以下更改:

 while (System.currentTimeMillis() < end) {
        WebElement resultsDiv = driver.findElement(By.id("gbqfbw"));
        resultsDiv.click();

        // If results have been returned, the results are displayed in a drop down.
        if (resultsDiv.isDisplayed()) {
          break;
        }
    }

您的代码无法找到搜索按钮,因为一旦它放入查询然后页面更改就这样了。

答案 1 :(得分:0)

进入上述与无法找到元素相同的问题。

然而,在检查发现的HTML时,搜索按钮上没有定义id。相反,它有一个名称属性定义为“btnG”,如下所示。

<button class="lsb" value="Search" aria-label="Google Search" name="btnG" type="submit"> <span class="sbico"></span> </button>

将while循环中的代码调整为以下内容,解决了问题。

while (System.currentTimeMillis() < end) {
            WebElement resultsDiv = driver.findElement(By.name("btnG"));
            resultsDiv.click();