Selenium2启动Chrome、Firefox、IE浏览器

本文介绍如何使用Selenium2自动化启动Chrome、Firefox和IE浏览器,并详细讲解了启动时的参数配置、加载插件以及特定浏览器的profile设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接https://www.cnblogs.com/yajing-zh/p/4897728.html

一、SELENIUM2启动浏览器

注意: SELENIUM2在启动浏览器时,都是启动一个干净的没有任务 插件及cookies信息的浏览器,即使是你之前的浏览器有设置过代理,到自动化启动时,也是没有代理的模式。

1.启动firefox浏览器:建议使用,要不有可能会出问题。

启动不在默认安装路径的firefox浏览器:

2.启动chrome浏览器:

需要chromedriver.exe的支持,驱动下载地址:(官方下载地址国内加速地址

如果不想用setProperty的方式,也可以将chromedriver.exe 放在”C:\Windows\System32”路径下并重启电脑即可

3.启动IE浏览器:
需要IEDriverServer.exe 的支持,且 IE的exe文件分64位与32位,请根据自已的机器选择相应的exe文件。
启动代码如下:

二、SELENIUM2启动浏览器时加载插件

加载插件的作用:有的是为了调试,也有的是被测试的系统本身需要插件

Firefox下加载firebug插件:

Chrome下加载crx插件:

且IE下没有插件加载。


三、SELENIUM2启动firefox时的profile设置

在firefox地址栏中输入about:config,可以看到有哪些可以设置

举例:导出文件的地址设置

3.启用默认情况下被firefox禁用的功能,以本地事件例,很简单直接设置为true就可以了。

特别重要:启动时,每次都是一个干净的firefox,如果要启动本机器的firefox的配置:

如果在机器B上要启动机器A上的firefox配置,则先导出A的配置,然后加载:

将A机器上的Profiles文件夹”C:\Users\cloudchen\AppData\Local\Mozilla\Firefox\Profiles”给拷贝出来

四、SELENIUM2启动chrome时的profile设置

特别重要:将user data文件夹” C:\Users\cloudchen\AppData\Local\Google\Chrome\User Data”拷贝出来


请自己验证一下,加不加以下代码的区别:

五、SELENIUM2启动IE时的设置

特别重要:启动IE时,需关闭保护模式。 4个红框标记的区域中的保护模式的checkbox 都要uncheck

也可以代码关闭:

完整代码如下:

package com.selenium.test;
 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
 
public class launchBrowser {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        launchChrome();
    }
 
    public static void launchFireFox() {
        FirefoxProfile firefoxProfile = new FirefoxProfile();
 
        firefoxProfile.setPreference("browser.download.folderList", 2);
        firefoxProfile.setPreference(
                "browser.download.manager.showWhenStarting", false);
        firefoxProfile.setPreference("browser.download.dir", "d:\\test");
        firefoxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "text/csv");
 
        String proxyIp = "child-prc.intel.com";
        int proxyPort = 913;
        firefoxProfile.setPreference("network.proxy.type", 1);
        firefoxProfile.setPreference("network.proxy.http", proxyIp);
        firefoxProfile.setPreference("network.proxy.http_port", proxyPort);
        firefoxProfile.setPreference("network.proxy.ssl", proxyIp);
        firefoxProfile.setPreference("network.proxy.ssl_port", proxyPort);
 
        firefoxProfile
                .setPreference("network.proxy.share_proxy_settings", true);
        firefoxProfile
                .setPreference("network.proxy.no_proxies_on", "localhost");
 
        WebDriver driver = new FirefoxDriver(firefoxProfile);
        // WebDriver driver = new FirefoxDriver();
        Navigation navigation = driver.navigate();
        navigation.to("https://baidu.com");
        // driver.close();
        // driver = null;
    }
 
    public static void launchChrome() {
        System.setProperty("webdriver.chrome.driver", "files/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        Navigation navigation = driver.navigate();
        navigation.to("https://www.baidu.com");
        driver.close();
        driver = null;
    }
 
    public static void launchIE() {
        System.setProperty("webdriver.ie.driver", "files/iedriver64.exe");
        WebDriver driver = new InternetExplorerDriver();
        Navigation navigation = driver.navigate();
        navigation.to("https://www.baidu.com");
        driver.close();
        driver = null;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值