2017-04-14 1 views
2

私はSelenium 3.3.1を使用しており、以下のコードをテストしています。次のエラーが表示され実行した後ドライバの実行可能ファイルへのパスは、webdriver.gecko.driverシステムプロパティで設定する必要があります。

:スレッド「メイン」java.lang.IllegalStateExceptionで

例外:ドライバの実行可能ファイルへのパスは webdriver.gecko.driverシステムプロパティによって設定されなければなりません;詳細については、 https://github.com/mozilla/geckodriverを参照してください。最新バージョンは は org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.javaで com.google.common.base.Preconditions.checkState(Preconditions.java:738) でhttps://github.com/mozilla/geckodriver/releasesからダウンロードできます。 における111) org.openqa.selenium.firefox.GeckoDriverService.access $ 100(GeckoDriverService.java:38) でorg.openqa.selenium.firefox.GeckoDriverService $ Builder.findDefaultExecutable(GeckoDriverService.java:112) でorg.openqa.selenium.remote.service.DriverService $ Builder.build(DriverService.java:302) at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:233) org.openqa.selenium.firefox.FirefoxDriverで。Selenium_loginで org.openqa.selenium.firefox.FirefoxDriver。(FirefoxDriver.java:121)で(FirefoxDriver.java:125) 。(Selenium_login.java:13 ) でSelenium_login.main(Selenium_login.java:70) /home/ali/.cache/netbeans/dev/executor-snippets/run.xml:53:Java が返されました:1 BUILD FAILED(合計時間:0秒)

Javaコード:

import java.io.*; 
import org.apache.commons.io.FileUtils; 
import org.openqa.selenium.By; 
import org.openqa.selenium.OutputType; 
import org.openqa.selenium.TakesScreenshot; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Selenium_login { 

    public WebDriver driver = new FirefoxDriver(); 

    /** 
    * Open the test website. 
    */ 
    public void openTestSite() { 
     driver.navigate().to("http://testing-ground.scraping.pro/login"); 
    } 

    /** 
    * 
    * @param username 
    * @param Password 
    * 
    *   Logins into the website, by entering provided username and 
    *   password 
    */ 
    public void login(String username, String Password) { 

     WebElement userName_editbox = driver.findElement(By.id("usr")); 
     WebElement password_editbox = driver.findElement(By.id("pwd")); 
     WebElement submit_button = driver.findElement(By.xpath("//input[@value='Login']")); 

     userName_editbox.sendKeys(username); 
     password_editbox.sendKeys(Password); 
     submit_button.click(); 

    } 

    /** 
    * grabs the status text and saves that into status.txt file 
    * 
    * @throws IOException 
    */ 
    public void getText() throws IOException { 
     String text = driver.findElement(By.xpath("//div[@id='case_login']/h3")).getText(); 
     Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("status.txt"), "utf-8")); 
     writer.write(text); 
     writer.close(); 

    } 

    /** 
    * Saves the screenshot 
    * 
    * @throws IOException 
    */ 
    public void saveScreenshot() throws IOException { 
     File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE); 
     FileUtils.copyFile(scrFile, new File("screenshot.png")); 
    } 

    public void closeBrowser() { 
     driver.close(); 
    } 

    public static void main(String[] args) throws IOException { 
     Selenium_login webSrcapper = new Selenium_login(); 
     webSrcapper.openTestSite(); 
     webSrcapper.login("admin", "12345"); 
     webSrcapper.getText(); 
     webSrcapper.saveScreenshot(); 
     webSrcapper.closeBrowser(); 
    } 
} 

答えて

4

セクレニアム3.0以降は、geckodriverをFirefoxとやりとりする必要があります。 geckodriverをOSに応じてgithubからダウンロードし、geckodriver.exeをフォルダに展開します。

WebDriverを初期化する前に、次の行を追加します

System.setProperty("webdriver.gecko.driver","c:/your/path/to/geckodriver.exe"); 
WebDriver driver = new FirefoxDriver(); 
... 
1

ヤモリドライバの実行可能ファイルのために例えばセレンでドライバーコンストラクタドライバ実行可能ファイルの

WebDriver driver = new FirefoxDriver(); 

検索し、この場合はFirefoxのドライバを検索します。場合にはこのサービスは、例外がどこから来るか、これは例外が

スローされる実行可能ファイルを見つけることができない(チェック状態方法に注意してください)

/** 
    * 
    * @param exeName Name of the executable file to look for in PATH 
    * @param exeProperty Name of a system property that specifies the path to the executable file 
    * @param exeDocs The link to the driver documentation page 
    * @param exeDownload The link to the driver download page 
    * 
    * @return The driver executable as a {@link File} object 
    * @throws IllegalStateException If the executable not found or cannot be executed 
    */ 
    protected static File findExecutable(
     String exeName, 
     String exeProperty, 
     String exeDocs, 
     String exeDownload) { 
    String defaultPath = new ExecutableFinder().find(exeName); 
    String exePath = System.getProperty(exeProperty, defaultPath); 
    checkState(exePath != null, 
     "The path to the driver executable must be set by the %s system property;" 
      + " for more information, see %s. " 
      + "The latest version can be downloaded from %s", 
      exeProperty, exeDocs, exeDownload); 

    File exe = new File(exePath); 
    checkExecutable(exe); 
    return exe; 
    } 

次の例外をスローチェック状態方法である

/** 
    * Ensures the truth of an expression involving the state of the calling instance, but not 
    * involving any parameters to the calling method. 
    * 
    * <p>See {@link #checkState(boolean, String, Object...)} for details. 
    */ 
    public static void checkState(
     boolean b, 
     @Nullable String errorMessageTemplate, 
     @Nullable Object p1, 
     @Nullable Object p2, 
     @Nullable Object p3) { 
    if (!b) { 
     throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3)); 
    } 
    } 

解決

を次のようにドライバ・オブジェクトを作成する前に、システムプロパティを設定します以下は

(FirefoxとChromeの)コードスニペットであるドライバの実行可能ファイルのドライバサービスを検索:

FireFoxの:

@Override 
protected File findDefaultExecutable() { 
     return findExecutable(
     "geckodriver", GECKO_DRIVER_EXE_PROPERTY, 
     "https://github.com/mozilla/geckodriver", 
     "https://github.com/mozilla/geckodriver/releases"); 
    } 

クローム:

@Override 
    protected File findDefaultExecutable() { 
     return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY, 
      "https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver", 
      "http://chromedriver.storage.googleapis.com/index.html"); 
    } 

ここで、GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver" とCHROME_DRIVER_EXE_PROPERTYが

類似=「webdriver.chrome.driver」は、以下、他のブラウザの場合では、利用可能なブラウザの実装

selenium browser support

のリストのスナップショットであります
関連する問題

 関連する問題