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();
        }
    }

    这样能成功运行

  • 相关阅读:
    JSP环境探针-当前电脑所有系统参数
    SqlServer service broker 分布式系统(赵松桃)跳水 2005 数据库编程
    主机Window不能访问该虚拟机Linux Samba文件服务提供了一个文件夹
    hdu 4901 The Romantic Hero
    linux、hdfs、hive、hbase经常使用的命令
    Android 设计模式模式适配器
    PHP扩展memcache模
    算法——字符串匹配Rabin-Karp算法
    三个重要的散列演示
    CodeForces 10C. Digital Root
  • 原文地址:https://www.cnblogs.com/yzq07231012/p/8058588.html
Copyright © 2011-2022 走看看