Selenium(最新)不工作

时间:2018-03-22 09:59:33

标签: java selenium selenium-webdriver

我正在努力让selenium使用eclipse和Java。所有产品(Selenium,Java,JDK,Eclipse)都是最新的,所有.jar文件都是在eclipse中导入的。 所以问题是一切正常,直到我想找到一个元素。我认为找到它有一个错误。但是在我的另一台计算机上,我安装了相同版本的产品,并且在那里工作。到现在为止,我是一个新手,为此约一周。

这是我的Java代码:

public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.setProperty("webdriver.gecko.driver", "C:\\Users\\yf0phgm\\eclipse\\Gecko\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        // Thread.sleep(3000);
        driver.get("http://www.google.de");
        //Wait for HTTP Proxy (Logged on by SSO)
        try {
            Thread.sleep(8000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // driver.findElement(By.partialLinkText("Über")).click();
        WebElement BtnClick = driver.findElement(By.name("btnI"));
        //
        BtnClick.click();
        // driver.quit();
}

控制台说:

1521712507113   geckodriver INFO    geckodriver 0.20.0 (0ac3698a74a7b1a742682b8e704f1f418df827ed 2018-03-13)

1521712507119   geckodriver INFO    Listening on 127.0.0.1:20002

1521712507795   mozrunner::runner   INFO    Running command: "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe" "-marionette" "-profile" "C:\\Users\\yf0phgm\\AppData\\Local\\Temp\\rust_mozprofile.dP85CqqTDXoP"

1521712508244   Marionette  INFO    Enabled via --marionette

1521712510272   Marionette  INFO    Listening on port 61771

1521712510502   Marionette  WARN    TLS certificate errors will be ignored for this session

Mär 22, 2018 10:55:10 AM org.openqa.selenium.remote.ProtocolHandshake createSession

INFORMATION: Detected dialect: W3C

Exception in thread "main" org.openqa.selenium.NoAlertPresentException: No modal dialog is currently open

Build info: version: '3.11.0', revision: 'e59cfb3', time: '2018-03-11T20:33:08.638Z'

System info: host: 'YF0PH011', ip: '10.103.2.160', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_161'

Driver info: org.openqa.selenium.firefox.FirefoxDriver

Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 59.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:headless: false, moz:processID: 8020, moz:profile: C:\Users\yf0phgm\AppData\Lo..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: XP, platformName: XP, platformVersion: 6.1, rotatable: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}

Session ID: 7b8e4c7c-e7a3-45d8-b97c-5e9beff67b1a

*** Element info: {Using=name, value=btnI}
    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.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:545)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:319)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:397)
    at org.openqa.selenium.By$ByName.findElement(By.java:303)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:311)
    at BasicPackage.OpenBrowser.main(OpenBrowser.java:25)

1 个答案:

答案 0 :(得分:0)

您可以尝试使用WebElement BtnClick = driver.findElement(By.name("btnI"));

,而不是像xpath中那样使用名称定位器
WebElement BtnClick = driver.findElement(By.xpath("//input[@name='btnI']"));

更新

我拿出了相同的代码并尝试使用By.name("btnI")By.xpath("//input[@name='btnI']")并获得了如下相同的输出:

package demo;

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

public class Google_Search_IamLucky_Button {

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("http://www.google.de");
        //WebElement BtnClick = driver.findElement(By.name("btnI"));
        WebElement BtnClick = driver.findElement(By.xpath("//input[@name='btnI']"));
        System.out.println("Identified -I am Feeling Lucky- button");
        BtnClick.click();
        System.out.println("Clicked on -I am Feeling Lucky- button");
        driver.quit();
        System.out.println("Quitted");
    }

}

以下是成功的控制台输出:

Identified -I am Feeling Lucky- button
Clicked on -I am Feeling Lucky- button
Quitted