selenium 2 铬驱动程序

Posted

技术标签:

【中文标题】selenium 2 铬驱动程序【英文标题】:selenium 2 chrome driver 【发布时间】:2011-11-19 00:12:09 【问题描述】:

所以我已经阅读了所有关于将 chromedriver 添加到我的路径的文档并遵循了所有这些文档。我在一台装有 selenium2、maven、eclipse 和所有最新驱动程序的 Mac 上:

Error:
The path to the chromedriver executable must be set by the webdriver.chrome.driver system property;

我将 chromedriver 放在我的 Applications 文件夹中,我的路径如下所示:

echo $PATH  
/Users/tcerrato/selenium/BS_Sel_Project/auto_helper/test_scripts:/usr/local/apache-maven-2.2.1//bin:/Users/oracle/oracle/product/10.2.0/db_1/bin:/opt/local/bin:/opt/local/sbin:/Applications:

我错过了什么?我根本无法使用 chrome 驱动程序运行。任何帮助都会很棒我现在正在尝试随机的东西。

这是我关于硒的 pom 部分:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium</artifactId>
    <version>2.0rc2</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-firefox-driver</artifactId>
    <version>2.6.0</version>
</dependency>

【问题讨论】:

【参考方案1】:

将WebDriverManager 添加到您的项目中:

<dependency>
   <groupId>io.github.bonigarcia</groupId>
   <artifactId>webdrivermanager</artifactId>
    <version>5.0.1</version>
</dependency>

该库下载您需要的最新版本的 WebDriver 二进制文件,并导出正确的 Java 系统变量(webdriver.chrome.driverwebdriver.opera.driverwebdriver.edge.driverwebdriver.ie.driver),只需使用以下之一分别是:

WebDriverManager.chromedriver().setup();
WebDriverManager.firefoxdriver().setup();
WebDriverManager.operadriver().setup();
WebDriverManager.edgedriver().setup();
WebDriverManager.iedriver().setup();

更多信息https://bonigarcia.dev/webdrivermanager/

【讨论】:

我尝试使用您的插件,但是虽然它确实下载了适当的二进制文件,但似乎 jbehave 总是尝试使用 firefix 驱动程序,即使使用系统属性驱动类 org.jbehave.web.selenium.PropertyWebDriverProvider Ok Jbehave 需要设置一个额外的系统属性:System.setProperty("browser", "chrome");如果您的设置允许您指定要使用 Jbehave 然后它代表您设置附加属性会很好,但如果我自己设置它会很好地工作。谢谢 这是否适用于不同的系统操作系统,如 Linux、Mac 等 确认使用 Selenium 3.x.x。非常感谢!【参考方案2】:

我不确定 Maven,但这是我设置属性 webdriver.chrome.driver 的方式

System.setProperty("webdriver.chrome.driver", "C:\\pathto\\my\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");

【讨论】:

那行得通。我读到的任何东西都没有让我很清楚几乎很容易:)谢谢 如何在 python 中做到这一点? @GregGauthier 根据 [chromedriver 的“入门”页面],您可以在创建 webdriver 时将路径指定为(可选)参数:driver = webdriver.Chrome('/path/to/chromedriver') @nilesh- 有没有办法在系统变量中设置 Chromedriver 路径并调用它?如何做到这一点 @nilesh 当我将ChromeDriver 定义为依赖项时。如何将属性 relativ 设置为我的 maven 项目,而不是设置绝对路径【参考方案3】:

通过 maven 设置 webdriver.chrome.driver 系统属性可以通过以下方式完成(并且经过测试):

    systemPropertyVariables 配置添加到pom.xml 中的maven-surefire-plugin。这(通常)是因为surefire 是测试的调用者,系统属性将在其中设置。

    <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.7.1</version>
        <configuration>
            <systemPropertyVariables>
                <webdriver.chrome.driver>$webdriver.chrome</webdriver.chrome.driver>
            </systemPropertyVariables>
        </configuration>
    </plugin>
    

    现在在某处定义$webdriver.chrome。一个好的开始是您的pom.xml 中的&lt;properties&gt; 部分

    <properties>
        <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome>
    </properties>
    

这可能会通过使用 &lt;profiles&gt; 来做得更好,就像在 Simon Martinelli 的 example 中一样

【讨论】:

【参考方案4】:

您可以尝试使用驱动程序二进制下载器 maven 插件为您下载驱动程序二进制文件 (https://github.com/Ardesco/selenium-standalone-server-plugin):

                <plugin>
                    <groupId>com.lazerycode.selenium</groupId>
                    <artifactId>driver-binary-downloader-maven-plugin</artifactId>
                    <version>1.0.7</version>
                    <configuration>
                        <rootStandaloneServerDirectory>$project.basedir/src/test/resources/selenium_standalone_binaries</rootStandaloneServerDirectory>
                        <downloadedZipFileDirectory>$project.basedir/src/test/resources/selenium_standalone_zips</downloadedZipFileDirectory>                            
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>selenium</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

这将下载二进制文件并设置一个 maven 属性,您可以像这样在安全/故障安全配置中使用该属性:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.7.2</version>
                    <configuration>                            
                        <systemProperties>                              
                            <!--Set properties passed in by the driver binary downloader-->
                            <phantomjs.binary.path>$phantomjs.binary.path</phantomjs.binary.path>
                            <webdriver.chrome.driver>$webdriver.chrome.driver</webdriver.chrome.driver>
                            <webdriver.ie.driver>$webdriver.ie.driver</webdriver.ie.driver>
                            <webdriver.opera.driver>$webdriver.opera.driver</webdriver.opera.driver>
                        </systemProperties>
                        <includes>
                            <include>**/*WebDriver.java</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

当您实例化一个新的驱动程序对象时,指向驱动程序二进制位置的系统属性现在将被设置并且它将正常工作。

【讨论】:

【参考方案5】:

所以在 pom 中你必须像这样设置它

                  <dependency>
                  <groupId>org.seleniumhq.selenium</groupId>
                  <artifactId>selenium-chrome-driver</artifactId>
                  <version>2.34.0</version>
                  </dependency>

这是一个使用 selenium 运行 chrome 的 java 代码

        System.setProperty("webdriver.chrome.driver","C:/chromedriver.exe");
        WebDriver myD = new ChromeDriver();

为了让您运行 Chrome,您需要从此处下载 chrome 驱动程序。 https://code.google.com/p/chromedriver/downloads/list

完成此操作后,您必须在环境变量中设置它。阅读此https://code.google.com/p/selenium/wiki/ChromeDriver

谢谢,

       Mediha

【讨论】:

那为什么还要使用 Maven 依赖呢? 同意,这里如果你把绝对路径放在代码中就不需要使用Maven依赖了。【参考方案6】:
System.setproperty("webdriver.chrome.driver","your file path here with chromedriver.exe");
webDriver driver=new chromeDriver();
driver.get("http://google.com");

【讨论】:

【参考方案7】:

试试这个:

System.setProperty("webdriver.chrome.driver","/location to/chromedriver folder");
WebDriver driver = new ChromeDriver();
driver.get("your.app");

【讨论】:

不是文件夹,而是chromedriver.exe的绝对路径。【参考方案8】:

它适用于我而无需设置 webdriver.chrome.driver 属性。只需将 chromedriver 添加到 PATH

> echo $PATH
/usr/local/bin:/usr/local/sbin:~/bin:/usr/bin:/bin:/usr/sbin:/sbin
>    
> which chromedriver
/usr/local/bin/chromedriver

如果您使用 Homebrew,安装 chromedriver 并添加到 PATH 可以像这样简单:

brew install chromedriver

有用的链接:

https://sites.google.com/a/chromium.org/chromedriver/

http://brewformulas.org/Chromedriver

【讨论】:

【参考方案9】:

只需在您的 maven pom 中添加 WebDriverManager,如果您在默认配置中设置了浏览器,它无需手动设置即可工作。

【讨论】:

【参考方案10】:
    Pom.xml code and Selenium code below:


   <groupId>com.HelloWorld</groupId>
   <artifactId>t</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <packaging>jar</packaging>

   <name>t</name>
   <url>http://maven.apache.org</url>

   <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>




     <webdriver.chrome>/home/gede/bin/chromedriver</webdriver.chrome>

     </properties>

     <build>
     <resources>
        <resource>
            <directory>src/main/java/resources</directory>
            <filtering>true</filtering> 
        </resource>
      </resources>
      <plugins>

      <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.7.1</version>
       <configuration>
        <systemPropertyVariables>
            <webdriver.chrome.driver>$webdriver.chrome
       </webdriver.chrome.driver>
        </systemPropertyVariables>
      </configuration>
       </plugin>


      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.20</version>
        <configuration>
          <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
        </configuration>
        </plugin>

      </plugins>


       </build>
       <dependencies>

       <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
      </dependency>

      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-
     chrome-driver -->
     <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-chrome-driver</artifactId>
    <version>3.8.1</version>
    </dependency>



       <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
      </dependency>  
      <dependency>
     <groupId>org.testng</groupId>
     <artifactId>testng</artifactId>
      <version>6.8</version>
     <scope>test</scope>
     </dependency>

     <dependency>   
            <groupId>org.seleniumhq.selenium</groupId>   
            <artifactId>selenium-chrome-driver</artifactId>   
            <version>3.8.1</version>   
        </dependency>   

        <dependency>
       <groupId>io.github.bonigarcia</groupId>
       <artifactId>webdrivermanager</artifactId>
      <version>2.1.0</version>
      </dependency>



      <dependency>
      <groupId>com.relevantcodes</groupId>
      <artifactId>extentreports</artifactId>
     <version>2.41.2</version>
     </dependency>



      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.8.2</version>
      </dependency>
      <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.8.2</version>
      </dependency>
     </dependencies>
</project>


 Selenuim Code 

public class App 

static String currentDir = System.getProperty("user.dir");
static WebDriver driver;

   @BeforeClass
    public static void setupClass() 
        ChromeDriverManager.getInstance().setup();
        driver= new ChromeDriver();
        driver.get("https://www.google.com/");
    


@Test
    public void test() 





    System.out.println( "Hello World!" );

    
  

【讨论】:

以上是关于selenium 2 铬驱动程序的主要内容,如果未能解决你的问题,请参考以下文章

Chrome 以“数据”打开;带硒铬驱动器

java 配置铬驱动器硒

有没有办法限制哪些表面铬可以屏幕记录?

如何防止在铬浏览器中自动填充同一主机上的表单?

铬禁用安全混乱的铬功能

未绑定断点 - VS Code |铬 |角