zoukankan      html  css  js  c++  java
  • Selenium安装问题

    1、selenium-java包是3.5.3版本

    2、代码实现的功能:打开Firefox浏览器,跳转到Google首页,然后输入hello selenium并提交搜索结果

    代码如下:

    package Selenium_Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Demo1 {
        public static void main(String[] args){
            WebDriver driver=new FirefoxDriver();
            driver.get("http://www.google.com.hk");
            WebElement element=driver.findElement(By.name("q"));
            element.sendKeys("hello selenium!");
            element.submit();
            try{
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("Page title is:"+driver.getTitle());
            driver.quit();
        }
    }

    但是运行完报错:Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: VISTA

    后面网上百度找答案,是因为火狐浏览器不是默认安装在C盘,需要只指定安装目录,加上这样一句代码(有的时候,安装在C盘,也要加上这样一句话):

    System.setProperty("webdriver.firefox.bin", "D:\Program Files (x86)\Mozilla Firefox\firefox.exe");//根据自己火狐应用程序的位置而定

    完整代码如下:

    package Selenium_Test;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class Demo1 {
        public static void main(String[] args){
            System.setProperty("webdriver.firefox.bin","D:\Program Files\Mozilla Firefox\firefox.exe");
            WebDriver driver=new FirefoxDriver();
            driver.get("http://www.google.com.hk");
            WebElement element=driver.findElement(By.name("q"));
            element.sendKeys("hello selenium!");
            element.submit();
            try{
                Thread.sleep(3000);
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            System.out.println("Page title is:"+driver.getTitle());
            driver.quit();
        }
    }

    这样能成功运行

  • 相关阅读:
    maven工程下的“run as application”
    Spark机器配置计算
    数学思路
    关联和依赖
    spark数据倾斜
    windows的DOS窗口如何修改大小
    MySQL的索引创建、删除
    使用composer命令创建laravel项目命令详解
    Windows平台查看端口占用情况
    使用composer安装laravel
  • 原文地址:https://www.cnblogs.com/yzq07231012/p/8058588.html
Copyright © 2011-2022 走看看