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

    这样能成功运行

  • 相关阅读:
    后端程序员之路 58、go wlog
    后端程序员之路 57、go json
    后端程序员之路 56、go package
    后端程序员之路 55、go redis
    后端程序员之路 54、go 日志库
    后端程序员之路 53、A Tour of Go-3
    后端程序员之路 52、A Tour of Go-2
    后端程序员之路 51、A Tour of Go-1
    后端程序员之路 50、Go语言开发环境
    后端程序员之路 49、SSDB
  • 原文地址:https://www.cnblogs.com/yzq07231012/p/8058588.html
Copyright © 2011-2022 走看看