b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

selenium webdriver学习(十五)

电脑杂谈  发布时间:2020-03-13 01:00:33  来源:网络整理

firefox50 selenium_selenium启动firefox_selenium firefox 驱动

这一节主要涉及 selenium webdriver处理Firefox profile的一些知识。

什么是Firefox profile

要知道Firefox profile请访问此处,它具体解绍了Firefox proflie。在Firefox里,如何管理Firefox profile 请访问此处。看完它们,相信你对Firefox profile会有所了解。好了,必备的知识准备完了,让我们来看看selenium webdriver 是如何操作Firefox profile的吧。

设置profile中的一个preference

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("aaa", "bbbb");
WebDriver driver = new FirefoxDriver(profile);

以上代码在Firefox Profile文件中设定一个名aaa,值为bbb的preference.(ps:这个preference只是一个举例,没有任何意义。要看firefox profile有什么preference,可以在firefox浏览器地址栏中键入:about:config). 代码运行后,在firefox浏览器地址栏中键入:about:config,可以看到它。

selenium启动firefox_firefox50 selenium_selenium firefox 驱动

启用已经存在的profile

首先来认识一下为什么应即将存在的profilefirefox50 selenium,其中一个原因是尚未存在的profile里面存放有cookie等信息,可以保持客户的登录状态。

启动已经存在的profile,因profile不同而有两种方式。一种是即使这个profile使用firefox配置管理器(Firefox's profile manager)而即将存在了。我们用以下的方式:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("WebDriver");
WebDriver driver = new FirefoxDriver(profile);

如果你想开启你平常用的firefox浏览器,可以把里面"WebDriver"替换成"default"firefox50 selenium,代码如下:

ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("default");
WebDriver driver = new FirefoxDriver(profile);

selenium启动firefox_firefox50 selenium_selenium firefox 驱动

另一种是没有在自己的firefox里面注册过的,比如从另一台机子中的firefox得到的,我们可以用以下的代码:

File profileDir = new File("path/to/your/profile");
FirefoxProfile profile = new FirefoxProfile(profileDir);
WebDriver driver = new FirefoxDriver(profile);

临时指定插件

有时我们必须临时让启动的firefox带一个插件,如firebug,来定位问题等。首先我们要下载这个软件的xpi安装包。剩下的就让selenium webdriver 来完成,如下:

File file = new File("path/to/your/firebug-1.8.1.xpi");
   FirefoxProfile firefoxProfile = new FirefoxProfile();
   firefoxProfile.addExtension(file);
   firefoxProfile.setPreference("extensions.firebug.currentVersion", "1.8.1"); //避免启面
   WebDriver driver = new FirefoxDriver(firefoxProfile);

selenium firefox 驱动_firefox50 selenium_selenium启动firefox

这样开启的firefox中就安装了软件firebug.

启用默认情况下被firefox禁用的功能

以本地事件例,很简单直接修改为true就可以了。

FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);

其它修改见selenium webdriver API中的org.openqa.selenium.firefox.FirefoxProfile.

启用firefox代理

selenium firefox 驱动_selenium启动firefox_firefox50 selenium

这个最简单,直接上代码了。

String PROXY = "localhost:8080";//如果不是本机,localhost替换成IP地址
org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
proxy.setHttpProxy(PROXY)
     .setFtpProxy(PROXY)
     .setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabailities();
cap.setPreference(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

over !

(转)Ruby的头号Gem:Rake|selenium webdriver学习(十四)---------- ...

1 楼blueskyzs2013-03-20

请问,在那可以查到FirefoxProfile的setPreference方法到底有什么preference啊?

我想在这里设置改变firefox的文件下载目录可以吗?

您还没有登录,请您登录后再发表评论


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-143954-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 高子芳
        高子芳

        主要是我们把买船的钱给老太后修了圆子

      • 曹髦
        曹髦

        存到银行缩水

      热点图片
      拼命载入中...