1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > selenium中启动firefox浏览器时设置代理 加载配置文件 加载某个插件以及firefox没有

selenium中启动firefox浏览器时设置代理 加载配置文件 加载某个插件以及firefox没有

时间:2023-03-26 01:20:37

相关推荐

selenium中启动firefox浏览器时设置代理 加载配置文件 加载某个插件以及firefox没有

selenium中启动firefox浏览器的六种方式,所用的是java

1、如果浏览器没有安装在默认路径下,则用

System.setProperty("Webdriver.firefox.bin","firefox浏览器的安装路径");

完整代码如下:

import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class KeywordBrowserFirefox {public static void main(String[] args) {System.setProperty("Webdriver.firefox.bin","C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");WebDriver driver = new FirefoxDriver();driver.get("");}}

2、如果firefox浏览器安装在默认路径下,直接用:

WebDriver driver = new FirefoxDriver();

完整代码如下:

import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;public class KeywordBrowserFirefox {public static void main(String[] args) {WebDriver driver = new FirefoxDriver();driver.get("");}}

3、用代理方式启动firefox浏览器

有时候某些网站需要用代理模式去访问,这个时候我们需要设置浏览器的代理,并且需要用户名与密码才能登陆代理服务器,那么就需要先获取到代理服务器的URL,然后输入用户名与密码,例如用代理的模式打开百度页面,具体在firefox浏览器上实现方式为:

import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;/** firefox代理*/public class KeywordBrowserProxy {public static void main(String[] args) {FirefoxProfile surrogate = new FirefoxProfile();// 启用代理模式surrogate.setPreference("network.proxy.type", 5);// 设置代理IPsurrogate.setPreference("network.proxy.http", "IP地址");// 设置代理端口号surrogate.setPreference("network.proxy.http_port", 8080);//启动firefox浏览器,并将surrogate对象传入WebDriver driver = new FirefoxDriver(surrogate);driver.get("");System.out.println(driver.getCurrentUrl());//如果需要用户名与密码才能登陆代理服务器,先获取到代理服务器的URL,然后再登陆driver.get(driver.getCurrentUrl());driver.manage().window().maximize();// 先定位到用户名与密码输入框,并输入用户名与密码WebElement name = driver.findElement(By.id("password_name"));name.sendKeys("用户名");WebElement pw = driver.findElement(By.id("password_pwd"));pw.sendKeys("密码");WebElement button = driver.findElement(By.id("password_submitBtn"));button.submit();driver.get("");}}

代理模式、IP地址、端口号,可以在firefox浏览器中输入about:config,然后再在搜索栏中输入network.proxy,如下图所示:

4、一般由selenium启动的firefox浏览器页面,是完全干净的页面,如果想启动firefox的一些配置文件,并且配置文件是本机的,配置文件的启动方式请看:firefox浏览器查看配置文件的三种方式:/postedit/81940854,firefox浏览器启动时加载配置文件,代码如下:

import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;import org.openqa.selenium.firefox.internal.ProfilesIni;/** firefox启动时加载本机的配置文件*/public class KeywordBrowserProfilesIni {public static void main(String[] args) {//创建profilesIni对象ProfilesIni ini = new ProfilesIni();//通过名字来获取到相应的配置文件FirefoxProfile profile = ini.getProfile("default");//创建浏览器驱动,并将profile传入,此时启动时,就会读取default配置文件来调用firefox浏览器了WebDriver driver = new FirefoxDriver(profile);driver.get("http:");}}

5、firefox浏览器启动时加载配置文件,如果配置文件不是本机的,是从别的地方拷贝过来的,代码如下:

import java.io.File;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;/** firefox浏览器启动时加载配置文件,如果配置文件是从其它地方复制过来的,保存在自己电脑E盘下configurationFiles*/public class KeywordBrowserProfilesPath {public static void main(String[] args) {//创建文件对象File file = new File("E:\\configurationFiles");//把复制过来的文件传入FirefoxProfile profile = new FirefoxProfile(file);WebDriver driver = new FirefoxDriver(profile);driver.get("");}}

6、firefox浏览器启动时,加载某个插件,如:firebug插件,先下载firebug插件,保存在D盘下,代码如下:

import java.io.File;import java.io.IOException;import org.openqa.selenium.WebDriver;import org.openqa.selenium.firefox.FirefoxDriver;import org.openqa.selenium.firefox.FirefoxProfile;/** firefox浏览器启动时加载firebug插件,先下载firebug插件,保存在D盘下*/public class KeywordBrowserPlug {public static void main(String[] args) throws IOException {//firebug插件存放在D盘下File file = new File("D:\\firebug_1487\\firebug");//创建profile对象FirefoxProfile profile = new FirefoxProfile(file);//调用FirefoxProfile类中addExtension方法profile.addExtension(file);//启动firefox浏览器并将profile对象传入WebDriver driver = new FirefoxDriver(profile);driver.get(""); }}

selenium中启动firefox浏览器时设置代理 加载配置文件 加载某个插件以及firefox没有安装在默认路径下的代码写法

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。