Selenium驱动Firefox浏览器

用Maven构建Selenium依赖

<dependency>    <groupId>org.seleniumhq.selenium</groupId>    <artifactId>selenium-java</artifactId>    <version>3.8.1</version></dependency>
import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;

public class Firefox {    public static void main(String[] args) throws InterruptedException {        System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe");        //指定Firefox浏览器驱动的路径,使用相对路径        String baiduHomePage;        baiduHomePage = "https://www.baidu.com/";        //百度首页的地址

WebDriver driver;        //声明一个WebDriver        driver = new FirefoxDriver();        driver.manage().window().maximize();        //使浏览器窗口最大化        driver.get(baiduHomePage);        //打开百度        Thread.sleep(2000);        //强制线程等待2秒钟        assert driver.getTitle().equals("百度一下,你就知道");        //断言页面标题

driver.findElement(By.xpath(".//*[@id=‘kw‘]")).sendKeys("Selenium");        //在百度搜索输入框输入“Selenium”        driver.findElement(By.xpath(".//*[@id=‘su‘]")).click();        //点击搜索按钮        Thread.sleep(2000);        assert driver.getTitle().equals("Selenium_百度搜索");

driver.close();        //关闭浏览器窗口        driver.quit();        //结束dirver    }}
需要注意的是,Firefox浏览器不能是官网上最新的版本,否则会出现启动了浏览器,却无法打开网址的情况;我用的版本是:Firefox-v52.5.3-win64驱动的版本是:geckodriver-v0.19.1-win64
 


原文地址:https://www.cnblogs.com/yjlch1016/p/8320792.html

时间: 2024-08-28 05:44:59

Selenium驱动Firefox浏览器的相关文章

解决Selenium与firefox浏览器版本不兼容问题

因为在用java打开firefox浏览器的时候报错 org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms 在网上查阅了说是兼容问题 然后就开始找selenium版本与对应firefox版本匹配的记录 各firefox版本下载地址 http://ftp.mozilla.org/pub/firefox/releases/ 各s

[乐意黎原创] 使用Selenium webdriver+Firefox浏览器来登录新浪微博

有个需求测试,需要用webdriver 登录到新浪微博,由于个人比较善长 Webdriver,于是采取了Webdriver+FireFox来实现. 配置环境 a. 必须首先在Eclipse里加载 selenium webdriver 包[[此处我导入的是 selenium-server-standalone-2.45.0.jar] b. 以及安装了 firefox 浏览器. 此篇文章中我用了本人的新浪微博地址来做测试,大家 可以更换为自己的新浪微博帐号和密码. 文中为部分主要的Java 代码.

爬虫动态渲染页面爬取之selenium驱动chrome浏览器的使用

Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样,可以用其进行网页动态渲染页面的爬取. 支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等. 1. 示例 ### selenium的使用 ''' Selenium是一个用于Web应用程序测试的工具. Selenium测试直接运行在浏览器中,就像真正的用户在操作一样. 支持的浏览器包括IE(7

如何使用selenium 驱动chrome浏览器并且打开方式为手机模式

随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要. 对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊. 使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码. public class Test{ public static void main(String args[]) { System.setProperty("we

Selenium驱动Chrome浏览器

import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.chrome.ChromeOptions; public class Chrome { public static void main(String[] args) throws InterruptedExceptio

Selenium之firefox浏览器的启动

1.编写如下代码 import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; /** * Created by Administrator on 2018/3/29 0029. */ public class FirefoxTest { public static void main(String[] args){ WebDriver driver; //启动找不到firefox才

python中使用selenium错误-Firefox浏览器

今天下午学习了一下编写自动化脚本,遇到的问题,记录一下. 解决方法:下载geckodriver.exe,将下载好的geckodriver.exe放入到D:\MyConfiguration\wtt42800\AppData\Local\Programs\Python\Python36-32(python的安装目录) 参考链接:http://jingyan.baidu.com/article/c910274b87b84acd361d2da3.html https://github.com/mozil

pycharm selenium 安装firefox驱动和Google驱动教程

一.下载Firefox浏览器或Google浏览器 下载渠道有很多,直接下载最新版的就可以了. 二.下载驱动 Firefox驱动 地址:https://github.com/mozilla/geckodriver/releases Firefox对应版本驱动: https://selenium-release.storage.googleapis.com/index.html 或Google驱动 Google对应版本驱动:-----需要用谷歌驱动的可以参照 谷歌驱动下载地址: http://npm

selenium,在Eclipse中打开fireFox浏览器报错:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms

1. 相信很多同学刚接触selenium时,在Eclipse中打开fireFox浏览器是报报错:org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:...(异常代码太多此处省略) 在网上找了好多方法都是不能解决问题,结果是焦头烂额! 2.其实这是selenium的jar包和