栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

robotframework Create Webdriver的一点理解

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

robotframework Create Webdriver的一点理解

1:先上官方文档说明:

Create Webdriver Arguments driver_name alias = None kwargs = {} ** init_kwargs Documentation

Creates an instance of Selenium WebDriver.

Like Open Browser, but allows passing arguments to the created WebDriver instance directly. This keyword should only be used if the functionality provided by Open Browser is not adequate.

driver_name must be a WebDriver implementation name like Firefox, Chrome, Ie, Opera, Safari, PhantomJS, or Remote.

The initialized WebDriver can be configured either with a Python dictionary kwargs or by using keyword arguments **init_kwargs. These arguments are passed directly to WebDriver without any processing. See Selenium API documentation for details about the supported arguments.

Examples:

# Use proxy with Firefox
${proxy}=Evaluateselenium.webdriver.Proxy()modules=selenium, selenium.webdriver
${proxy.http_proxy}=Set Variablelocalhost:8888
Create WebdriverFirefoxproxy=${proxy}
# Use proxy with PhantomJS
${service args}=Create List--proxy=192.168.132.104:8888
Create WebdriverPhantomJSservice_args=${service args}

Returns the index of this browser instance which can be used later to switch back to it. Index starts from 1 and is reset back to it when Close All Browsers keyword is used. See Switch Browser for an example.

2:实践理解
这边有两个例子,为谷歌和火狐添加useragent,并且是远端调用方式:

Chrome:

     ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
     ${user_agent_option}=  set variable  --user-agent=${UserAgent}
     Call Method    ${options}  add_argument  ${user_agent_option}
     Create WebDriver    Remote    command_executor="http://xx:4444/wd/hub"    options=${options}
     Go to    ${URL}

Firefox:

     ${class_options}=    Evaluate    sys.modules['selenium.webdriver'].FirefoxOptions()  sys, selenium.webdriver
     ${user_agent_option}=  set variable  userAgent=${UserAgent}
     Call Method    ${class_options}  set_preference  general.useragent.override    ${user_agent_option}
     Create WebDriver    Remote    command_executor="http://xx:4444/wd/hub"    options=${class_options}
     Go to    ${URL}

逐行解释

     ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
     ${user_agent_option}=  set variable  --user-agent=${UserAgent}
     Call Method    ${options}  add_argument  ${user_agent_option}

这部分就是在robotframework里执行selenium里的webdriver添加options的功能
形如下面selenium里的添加options

from selenium import webdriver
import os
 
# 进入浏览器设置
options = webdriver.ChromeOptions()
# 谷歌无头模式
options.add_argument('--headless')
options.add_argument('--disable-gpu')
# 设置页面大小
options.add_argument('window-size=1200x600')
# 设置中文
options.add_argument('lang=zh_CN.UTF-8')
# 更换头部
options.add_argument('user-agent="Mozilla/5.0 (iPod; U; CPU iPhone OS 2_1 like Mac OS X; ja-jp) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5F137 Safari/525.20"')
# 设置代理
options.add_argument('proxy-server=' + proxy)
# 不加载图片
prefs = {"profile.managed_default_content_settings.images":2}
options.add_experimental_option("prefs",prefs)
# 导入当前py文件目录下的chromedriver
executable_path = os.path.abspath('chromedriver.exe')
 
browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
url = "https://httpbin.org/get?show_env=1"
browser.get(url)
# 设置浏览器窗口大小
browser.set_window_size(1552, 800)
# 删除原来的cookie
browser.delete_all_cookies()
# 添加cookie
browser.add_cookie({'name':'ABC','value':'DEF'})
# 刷新当前页面
browser.refresh()
# 关闭当前窗口
browser.close()
# 关闭浏览器
browser.quit()

下面这部分就是实例化一个webdriver,实例化的时候传入远端调用参数以及options,最后并打开一个url。Create WebDriver Remote 这里创建的是remote类型的,如果不是远端调用,就写具体的浏览器名称。

Create WebDriver    Remote    command_executor="http://xx:4444/wd/hub"    options=${class_options}
     Go to    ${URL}

形如下面selenium里的实例化webdriver

    chrome_capabilities = {"browserName": "chrome",
                           "version": "",
                           "platform": "ANY",
                           "javascriptEnabled": True,
                           }

    browser = Remote(command_executor='http://xxx:5555/wd/hub',
                    desired_capabilities=chrome_capabilities)

火狐浏览器那部分和谷歌浏览器有些区别,是因为selenium给火狐浏览器添加options内容上有区别。
selenium里添加如下:

from selenium.webdriver.common.proxy import * 
# 代理
myProxy = '127.0.0.1:8080'
# 代理格式
proxy = Proxy({
  'proxyType': ProxyType.MANUAL, 
  'httpProxy': myProxy, 
  'ftpProxy': myProxy, 
  'sslProxy': myProxy, 
  'noProxy': ''
 })
 
profile = webdriver.FirefoxProfile()
profile = get_firefox_profile_with_proxy_set(profile, proxy)
profile.set_preference("general.useragent.override", user_agent)
 
# firefox无头模式
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('window-size=1200x600')
executable_path = os.path.abspath('geckodriver.exe')
 
#无图模式
options.set_preference('permissions.default.image',2)
 
driver=webdriver.Firefox(proxy=proxy, profile=profile, 
						 options=options, executable_path=executable_path) 
driver.get('https://www.baidu.com') 
driver.quit()

总结:Open Browser就是包含了Create Webdriver和Go to ${URL}的二次封装。当Open Browser不能满足的时候,可以用Create Webdriver试试,以最原始的方式去创建webdriver实例。SeleniumLibrary还是依赖selenium的实现方式去实现的相应功能。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/980192.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号