热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

开发笔记:seleniumjava浏览器操作

篇首语:本文由编程笔记#小编为大家整理,主要介绍了seleniumjava浏览器操作相关的知识,希望对你有一定的参考价值。一环境搭建

篇首语:本文由编程笔记#小编为大家整理,主要介绍了selenium java 浏览器操作相关的知识,希望对你有一定的参考价值。


一 环境搭建

selenium 2.53

selenium-java-2.53.0.jar

selenium-java-2.53.0-srcs.jar 原代码包

拷贝的工程lib下,做build path,告诉项目jar包在lib里

关联原始代码:

jar包里都是.class文件,想看原始代码,关联源代码包,在selenium项目包右键属性,选java source attachment,选择selenium-java-2.53.0-srcs.jar。

package com.thoughtworks.selenium 源代码

package org.openqa.selenium 开源的代码

 

一个driver是一个浏览器实例,

selenium2.4.5-javadoc/index.html: api文档

-org.openqa.selenium.firefox 存放的是Firefox浏览器使用的类



  • --FirefoxBinary 

  • --FirefoxDriver 浏览器实例

  • --FirefoxProfile 用户配置文件

构造方法重载:不同的方式构造实例


FirefoxDriver()
FirefoxDriver(Capabilities desiredCapabilities)
FirefoxDriver(Capabilities desiredCapabilities, Capabilities requiredCapabilities)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities capabilities)
FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile, Capabilities desiredCapabilities, Capabilities requiredCapabilities)
FirefoxDriver(FirefoxProfile profile)


package test.selenium;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class SeleniumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = new FirefoxDriver();
driver.
get("http://nvsm.cnki.net/KNS/");
driver.close();
}
}

 如果出现打不开浏览器,地址栏不能输入,操作不了页面dom,都是selenium版本和浏览器版本不匹配。selenium更新版本比浏览器慢,不能保证支持最新的浏览器版本。selenium2.53可以支持Firefox40以下版本。

 二 Webdriver基础API - 浏览器实例管理




    • -org.openqa.selenium.ie

    • --InternetExplorerDriver


      • org.openqa.selenium.chrome

      • --ChromeDriver


      • public class FirefoxDriver extends RemoteWebDriver
        public class RemoteWebDriver extends java.lang.Object implements WebDriver


      • 如果需要用的Firefox特殊的方法,需强制类型转换

      • appium和selenium用的是一套webdriver协议(多态)






    • 执行程序每次打开的都是一个全新的没有设置的Firefox浏览器,设置不会在下次生效。selenium只会在默认路径(C:\Program Files (x86)\Mozilla Firefox\firefox.exe)找Firefox,如果安装位置不在默认路径则会报找不到Firefox可执行文件的错误。

    • 举例:让selenium找的Firefox安装位置







System.setProperty("webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
WebDriver driver
= new FirefoxDriver();
driver.
get("http://nvsm.cnki.net/KNS/");

 

举例:构造浏览器实例

FirefoxDriver(FirefoxBinary binary, FirefoxProfile profile) 

FirefoxBinary(java.io.File pathToFirefoxBinary) 


FirefoxBinary ffb = new FirefoxBinary(new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"));
WebDriver driver
= new FirefoxDriver(ffb,null);
driver.
get("http://nvsm.cnki.net/KNS/");

 

举例:构造浏览器实例(带配置文件的情况,利用存在的profile文件)

 FirefoxProfile(java.io.File profileDir)

FirefoxProfile作用:保存用户配置信息,包括:浏览器设置信息,插件以及设置,书签和历史记录,用户名和密码,临时文件和网站数据,COOKIEs。

cmd命令行输入:firefox.exe -ProfileManage


C:\Program Files (x86)\Mozilla Firefox>firefox.exe -ProfileManage

运行输入:%APPDATA%,打开C:\Users\PC\AppData\Roaming,所有的用户配置信息都在:C:\Users\PC\AppData\Roaming\Mozilla\Firefox\Profiles\1h1iplez.default


FirefoxProfile ffp = new FirefoxProfile(new File("C:\\Users\\PC\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\1h1iplez.default"));
WebDriver driver
= new FirefoxDriver(ffp);
driver.
get("http://nvsm.cnki.net/KNS/");

 

举例:构造浏览器实例(带配置文件的情况,自定义profile文件启动)

FirefoxProfile有3个重载的方法:

















voidsetPreference(java.lang.String key, boolean value)
Set a preference for this particular profile.

voidsetPreference(java.lang.String key, int value)
Set a preference for this particular profile.

voidsetPreference(java.lang.String key, java.lang.String value)
Set a preference for this particular profile.

浏览器地址栏输入:about:config,设置所有的属性和插件配置

浏览器相关配置:browser 

例如:设置启动默认页面:browser.startup.homepage


FirefoxProfile ffp = new FirefoxProfile();
ffp.setPreference(
"browser.startup.homepage", "http://nvsm.cnki.net/KNS/"); //设置启动页
ffp.setPreference("browser.startup.page", 1); //0空白首页设置不生效 1用户自定义首页
WebDriver driver = new FirefoxDriver(ffp);

 Webdriver设置Firefox无提示默认路径下载


FirefoxProfile ffp = new FirefoxProfile();
ffp.setPreference(
"browser.download.dir", "C:\\");
ffp.setPreference(
"browser.download.folderList", 2);
// 设置浏览器默认下载文件夹 0桌面 1我的下载 2自定义
ffp.setPreference("browser.download.manager.showWhenStarting", false);
ffp.setPreference(
"browser.helperApps.neverAsk.saveToDisk", "application/octet-stream, application/vnd.ms-excel, text/csv, application/zip");
WebDriver driver
= new FirefoxDriver(ffp);

 firebug插件,网络查看资源包括静态资源和js代码加载快慢,刷新地址可生成网络布图,资源加载的时间,参数可以输出成har文件,可以用showslow打开

举例:自动收集页面加载时序图


FirefoxProfile ffp = new FirefoxProfile();
ffp.addExtension(
new File("source\\firebug-2.0.17.xpi"));
ffp.addExtension(
new File("source\\netExport-0.8.xpi"));
// 插件相关
ffp.setPreference("extensions.firebug.allPagesActivation", "on"); //所有页面自动开启
ffp.setPreference("extensions.firebug.net.enableSites", "true"); //网络设置成开启
ffp.setPreference("extensions.firebug.defaultPanelName","net");
ffp.setPreference(
"extensions.firebug.netexport.alwaysEnableAutoExport","true");
ffp.setPreference(
"extensions.firebug.netexport.saveFiles","true");
ffp.setPreference(
"extensions.firebug.netexport.defaultLogDir", "C:\\");
WebDriver driver
= new FirefoxDriver(ffp);
Thread.sleep(
2000);
driver.
get("http://nvsm.cnki.net/KNS/");

 



推荐阅读
  • 如何实现JDK版本的切换功能,解决开发环境冲突问题
    本文介绍了在开发过程中遇到JDK版本冲突的情况,以及如何通过修改环境变量实现JDK版本的切换功能,解决开发环境冲突的问题。通过合理的切换环境,可以更好地进行项目开发。同时,提醒读者注意不仅限于1.7和1.8版本的转换,还要适应不同项目和个人开发习惯的需求。 ... [详细]
  • 本文介绍了OkHttp3的基本使用和特性,包括支持HTTP/2、连接池、GZIP压缩、缓存等功能。同时还提到了OkHttp3的适用平台和源码阅读计划。文章还介绍了OkHttp3的请求/响应API的设计和使用方式,包括阻塞式的同步请求和带回调的异步请求。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Java 11相对于Java 8,OptaPlanner性能提升有多大?
    本文通过基准测试比较了Java 11和Java 8对OptaPlanner的性能提升。测试结果表明,在相同的硬件环境下,Java 11相对于Java 8在垃圾回收方面表现更好,从而提升了OptaPlanner的性能。 ... [详细]
  • 开发笔记:spring boot项目打成war包部署到服务器的步骤与注意事项
    本文介绍了将spring boot项目打成war包并部署到服务器的步骤与注意事项。通过本文的学习,读者可以了解到如何将spring boot项目打包成war包,并成功地部署到服务器上。 ... [详细]
  • 本文介绍了在sqoop1.4.*版本中,如何实现自定义分隔符的方法及步骤。通过修改sqoop生成的java文件,并重新编译,可以满足实际开发中对分隔符的需求。具体步骤包括修改java文件中的一行代码,重新编译所需的hadoop包等。详细步骤和编译方法在本文中都有详细说明。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • Apache Shiro 身份验证绕过漏洞 (CVE202011989) 详细解析及防范措施
    本文详细解析了Apache Shiro 身份验证绕过漏洞 (CVE202011989) 的原理和影响,并提供了相应的防范措施。Apache Shiro 是一个强大且易用的Java安全框架,常用于执行身份验证、授权、密码和会话管理。在Apache Shiro 1.5.3之前的版本中,与Spring控制器一起使用时,存在特制请求可能导致身份验证绕过的漏洞。本文还介绍了该漏洞的具体细节,并给出了防范该漏洞的建议措施。 ... [详细]
  • 本文介绍了解决java开源项目apache commons email简单使用报错的方法,包括使用正确的JAR包和正确的代码配置,以及相关参数的设置。详细介绍了如何使用apache commons email发送邮件。 ... [详细]
  • 1.脚本功能1)自动替换jar包中的配置文件。2)自动备份老版本的Jar包3)自动判断是初次启动还是更新服务2.脚本准备进入ho ... [详细]
  • Jmeter对RabbitMQ压力测试
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了Jmeter对RabbitMQ压力测试相关的知识,希望对你有一定的参考价值。Jm ... [详细]
  • android ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
author-avatar
阿思翠
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有