Selenium 学习笔记(Java版)(一)永远的HelloWorld

Selenium 学习笔记(Java版)(一)永远的HelloWorld

大家好,又见面了,我是全栈君。

在pom文件中添加selenium依赖

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.4.0</version>
</dependency>

本文使用Firefox和Chrome两种最常用的浏览器

代码执行的效果是打开浏览器,进入百度,自动输入hello Selenium然后搜索

(1)使用Firefox

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HelloTest1 {
    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "D:\\1Program\\Selenium_Driver\\geckodriver.exe");
        System.setProperty("webdriver.firefox.bin", "E:\\Mozilla Firefox\\firefox.exe");
        WebDriver dr =  new FirefoxDriver();
        dr.get("http://www.baidu.com");
        dr.findElement(By.id("kw")).sendKeys("hello Selenium");
        dr.findElement(By.id("su")).click();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

本文只介绍前几行,剩余部分在后续文章介绍

System.setProperty("webdriver.gecko.driver", "D:\\1Program\\Selenium_Driver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "E:\\Mozilla Firefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();

使用FirefoxDriver时,程序会去默认路径寻找firefox浏览器,大部分人可能都不是默认的,所以需要通过webdriver.firefox.bin设置系统变量,这时候直接运行会报如下错误

Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
	at com.google.common.base.Preconditions.checkState(Preconditions.java:738)
	at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
	at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
	at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
	at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
	at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108)
	at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104)
	at com.mysite.selenium.HelloTest1.main(HelloTest1.java:11)

于是去下载geckodriver,然后设置路径到webdriver.gecko.driver即可(PS:看网上的说法,似乎设置到环境变量也可以,各位可以试一下)

(2)使用Chrome

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HelloTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "D:\\1Program\\Chrome_Driver\\chromedriver.exe");

        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com");
        driver.findElement(By.id("kw")).sendKeys("hello Selenium");
        driver.findElement(By.id("su")).click();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //driver.quit();
    }
}

Chrome浏览器需要单独下载driver,地址在这里(需要梯子),然后设置系统变量webdriver.chrome.driver即可

 

转载于:https://my.oschina.net/icebergxty/blog/913825

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108444.html原文链接:https://javaforall.net

(0)
上一篇 2022年3月3日 下午10:00
下一篇 2022年3月3日 下午10:00


相关推荐

  • bootefi分多大合适_华硕secure boot在哪

    bootefi分多大合适_华硕secure boot在哪一、DM36X的BOOT过程介绍DM36x的BOOT过程和DM6446、DM6467完全是一样的,因为都是ARM926EJS架构,里边都有一个RBL,这RBL在芯片出厂的时候都烧写在ROM里,芯片上电复位后RBL在运行,然后读取BOOTMODE引脚的电平状态,决定是从NANDFLASH还是从MMC/SD、UART、USB、SPI、EMAC、HPI中之一的方式BOOT,具体见SPRS

    2022年8月13日
    15
  • SpringBoot集成Swagger3 —— 教你如何优雅的写文档

    SpringBoot集成Swagger3 —— 教你如何优雅的写文档Swagger 介绍及使用导语 相信无论是前端还是后端开发 都或多或少地被接口文档折磨过 前端经常抱怨后端给的接口文档与实际情况不一致 后端又觉得编写及维护接口文档会耗费不少精力 经常来不及更新 其实无论是前端调用后端 还是后端调用后端 都期望有一个好的接口文档 但是这个接口文档对于程序员来说 就跟注释一样 经常会抱怨别人写的代码没有写注释 然而自己写起代码起来 最讨厌的 也是写注释 所以仅仅只通过强制来规范大家是不够的 随着时间推移 版本迭代 接口文档往往很容易就跟不上代码了 Swagger 简介

    2026年3月18日
    3
  • quotename mysql,我如何在mySQL中创建一个QUOTENAME函数

    quotename mysql,我如何在mySQL中创建一个QUOTENAME函数IwouldliketocreateaQUOTENAME()functioninmySQLliketheonethatexistsinM$SQLServer.Thisiswhatitdoes:QUOTENAMEreturnsaUnicodestringwiththedelimitersaddedtomaketheinputstr…

    2022年7月25日
    15
  • Redis线程模型

    Redis线程模型一 概述 Redis 线程模型主要是基于 Reactor 模式开发的网络事件处理器 这个处理器被称为文件事件处理器 fileeventhan 也就是说 Redis 线程模型指的就是文件事件处理器 文件事件处理器是单线程模式运行的 所以也叫单线程模型 但是通过 IO 多路复用机制监听多个 socket 可以实现高性能的网络通信模型 又可以跟内部其他单线程的模块进行对接 保证了 redis 内部的线程模型的简单性 二 文件事件类型文件事件有两种类型 读事件 AE READABLE 写事件 AE WRITABLE

    2026年3月19日
    2
  • 怎么新建pytest的ini文件_pytest.ini配置

    怎么新建pytest的ini文件_pytest.ini配置前言pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行查看pytest.ini的配置选项pytest-h找到以下

    2022年7月30日
    6
  • 科大讯飞发布星火X1.5大模型 推理效率较上代提升一倍

    科大讯飞发布星火X1.5大模型 推理效率较上代提升一倍

    2026年3月14日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号