python webdriver api-对启动的火狐浏览器添加配置

Webdriver启用的火狐不带插件,可以自已进行配置

先找到火狐的安装路径

C:Program FilesMozilla Firefox

步骤说明

在CMD中使用cd命令进入firefox.exe文件所在目录(比如:C:Program FilesMozilla Firefox),
并输入firefox.exe -ProfileManager -no-remote命令,然后按Enter键,
调出“Firefox – 选择用户配置文件”操作窗口


如果firefox.exe -ProfileManager -no-remote 执行弹出一个页面说找不到路径,解决方法:
在火狐的菜单“帮助”下,选择“故障排除信息”,点击后,在弹出的页面中找到“配置文件夹 ”的
选项,点击“打开文件夹”,可以获取默认配置文件的全路径。


进入mac的火狐路径:/Applications/Firefox.app/Contents/MacOS
执行:firefox -profilemanager 
新建profile

调出配置窗口,cmd下进入火狐的路径,执行命令firefox.exe -ProfileManager -no-remote

选择启动时不询问并使用选定的配置文件

可以创建配置文件

在创建的时候找到刚才做了配置的账户的路径

找到刚才做了配置的账户的路径替换到python文件里

C:UsersLenovoAppDataRoamingMozillaFirefoxProfileseuhvixdt.default

#encoding=utf-8

from selenium import webdriver

from selenium.common.exceptions import NoSuchElementException

import unittest, time

class TestFailCaptureScreen(unittest.TestCase):

    def setUp(self):

        # 创建存储自定义配置文件的路径变量

        #proPath = "C:\Users\wuxiaohua\AppData\Roaming\Mozilla\Firefox\Profiles\tbbmxtkv.webdriver"

        proPath = "C:UsersLenovoAppDataRoamingMozillaFirefoxProfileseuhvixdt.default"

        # 加载自定义配置文件到FirefoxProfile实例中,

        # 等价profile = webdriver.FirefoxProfile(proPath)

        profile = webdriver.firefox.firefox_profile.FirefoxProfile(proPath)

        # 将添加了新配置文件的Firefox浏览器首页设为搜狗主页

        profile.set_preference("browser.startup.homepage", "http://www.sogou.com")

        # 设置开始页面不是空白页,0表示空白页,

        # 这一步必须做,否则设置的主页不会生效

        profile.set_preference("browser.startup.page", 1)

        # 启动带自定义配置文件的Firefox浏览器

        self.driver = webdriver.Firefox(executable_path="d:\geckodriver", firefox_profile=profile)

    def testSoGouSearch(self):

        # 等待5秒,以便浏览器启动完成

        time.sleep(5)

        try:

            # 找到搜狗主页搜索输入框页面元素

            searchBox = self.driver.find_element_by_id("query")

            # 在找到的搜索输入框中输入“光荣之路自动化测试”

            searchBox.send_keys(u"光荣之路自动化测试")

            # 找到搜索按钮,并点击

            self.driver.find_element_by_id("stb").click()

            time.sleep(10)

        except NoSuchElementException, e:

            print "修改带自定义配置文件的浏览器主页不成功!"

    def tearDown(self):

        # 退出Firefox浏览器

        self.driver.quit()

if __name__ == '__main__':

unittest.main()

D: est>python test.py

.

----------------------------------------------------------------------

Ran 1 test in 52.252s

OK

转载来自:https://www.cnblogs.com/xiaxiaoxu/p/9206341.html

原文地址:https://www.cnblogs.com/yblafxw/p/10483172.html