Selenium FireFoxDriver无法连接

时间:2016-08-24 16:47:46

标签: java selenium firefox geckodriver

我一直在尝试使用Selenium来推动Firefox。我使用了几乎相同的代码来驱动Chrome而没有问题。但是,当我尝试使用Firefox驱动程序时,浏览器会打开,停止,然后在大约60秒后,我会收到一条错误报告,内容如下:

Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox   console output:
4474-a285-3208198ce6fd}","syncGUID":"dcskEFBTLyBH","location":"app-global","version":"48.0.1","type":"theme","internalName":"classic/1.0","updateURL":null,"updateKey":null,"optionsURL":null,"optionsType":null,"aboutURL":null,"icons":{"32":"icon.png","48":"icon.png"},"iconURL":null,"icon64URL":null,"defaultLocale":{"name":"Default","description":"The default theme.","creator":"Mozilla","homepageURL":null,"contributors":["Mozilla Contributors"]},"visible":true,"active":true,"userDisabled":false,"appDisabled":false,"descriptor":"C:\\Program Files\\Mozilla Firefox\\browser\\extensions\\{972ce4c6-7e08-4474-a285-3208198ce6fd}.xpi","installDate":1471881400240,"updateDate":1471881400240,"applyBackgroundUpdates":1,"skinnable":true,"size":21905,"sourceURI":null,"releaseNotesURI":null,"softDisabled":false,"foreignInstall":false,"hasBinaryComponents":false,"strictCompatibility":true,"locales":[],"targetApplications":[{"id":"{ec8030f7-c20a-464f-9b0e-13a3a9e97384}","minVersion":"48.0.1","maxVersion":"48.0.1"}],"targetPlatforms":[],"seen":true}
1472056603181   addons.xpi  DEBUG   getModTime: Recursive scan of {972ce4c6-7e08-4474-a285-3208198ce6fd}

我已经检查了其他指南,他们建议我更新我的.jar文件。我正在使用selenium-java-3.0.0-beta2和Firefox 48.0.1进行测试,因此我的文件是最新的。我想让它正常运行。

更新:代码仍然无效,我已设置System属性以正确设置geckodriver。但是,我仍然无法让驱动程序正常运行。它甚至不再启动浏览器了。

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SimpleFireFoxDriver {


public static void main(String[] args) {

    System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver.exe");

    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    capabilities.setCapability("marionette", true);

   WebDriver driver = new FirefoxDriver();

   driver.get("http://www.youtube.com");

   System.out.println("Made it to the promised land");

   driver.quit();

    }

}

编辑:FireFox本身的路径也位于此处: “C:\ Program Files \ Mozilla Firefox \ firefox.exe”

5 个答案:

答案 0 :(得分:2)

更改系统属性对我有用。将其更改为以下内容:

   test = "hello"
   # Open database connection
    db = MySQLdb.connect("127.0.0.1","admin","password","table" )

    # prepare a cursor object using cursor() method
    cursor = db.cursor()

    # Prepare SQL query to INSERT a record into the database.
    sql = ("""INSERT INTO %s (name,
            age, gender)
            VALUES (%s, %s, %s)""",(test))
    try:
        # Execute the SQL command
        cursor.execute(sql, (name, age, gender))
        db.commit()
    except:
        raise
        db.rollback()


    # disconnect from server
        db.close()

希望有所帮助。

答案 1 :(得分:1)

这种情况正在发生,因为您设置了错误的系统属性。您需要按如下方式设置系统属性:

System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver.exe");

Selenium firefox驱动程序希望在启动木偶驱动程序和启动firefox之前设置此System属性。如果您没有设置任何系统属性并尝试实例化Firefox驱动程序,那么您将收到以下错误: "驱动程序可执行文件的路径必须由webdriver.gecko.driver系统属性设置。"

希望这有帮助。

答案 2 :(得分:1)

更改" webdriver.gecko.driver "使用" webdriver.firefox.marionette "救了我的命!  例如:

<强>正确

System.setProperty("webdriver.firefox.marionette","C://selenium/gecko/geckodriver.exe");

不正确

System.setProperty("webdriver.gecko.driver","C://selenium/gecko/geckodriver.exe");

答案 3 :(得分:0)

更改&#34; macro_rules! foo { ($($(#[$meta:meta])* foo $name:ident($ty:ty),)*) => { #[derive(Debug, Clone)] pub enum Foo { $($(#[$meta])* $name($ty),)* } impl Foo { pub fn display(&self) { match *self { $(Foo::$name(x) => println!("{}", x),)* } } } } } foo! { foo A(i32), foo B(i64), } fn main() { let a = Foo::A(32); let b = Foo::B(64); a.display(); b.display(); } &#34;用&#34; webdriver.gecko.driver&#34;救了我的生命也将Firefox从50降级到36

或尝试此代码:

webdriver.firefox.marionette

答案 4 :(得分:0)

下载最新的Gecko驱动程序V0.17.0,这解决了我的错误,而没有更改setProperty或降级Firefox浏览器。

不太确定,这是否对您有帮助。