robotframework Create Webdriver的一点理解

1:先上官方文档说明:

Arguments
driver_name <str> alias
= None
<str><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的实现方式去实现的相应功能。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值