robotframework Create Webdriver的一点理解

阅读: 评论:0

robotframework Create Webdriver的一点理解

robotframework Create Webdriver的一点理解

1:先上官方文档说明:

Create Webdriver

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    dules['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="xx:4444/wd/hub"    options=${options}Go to    ${URL}

Firefox:

     ${class_options}=    Evaluate    dules['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="xx:4444/wd/hub"    options=${class_options}Go to    ${URL}

逐行解释

     ${options}=    Evaluate    dules['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(&#')browser = webdriver.Chrome(executable_path=executable_path, chrome_options=options)
url = "=1"
(url)
# 设置浏览器窗口大小
browser.set_window_size(1552, 800)
# 删除原来的cookie
browser.delete_all_cookies()
# 添加cookie
browser.add_cookie({'name':'ABC','value':'DEF'})
# 刷新当前页面
fresh()
# 关闭当前窗口
browser.close()
# 关闭浏览器
browser.quit()

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

Create WebDriver    Remote    command_executor="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='xxx:5555/wd/hub',desired_capabilities=chrome_capabilities)

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

from selenium.webdrivermon.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(&#')#无图模式
options.set_preference('permissions.default.image',2)driver=webdriver.Firefox(proxy=proxy, profile=profile, options=options, executable_path=executable_path) 
('') 
driver.quit()

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

本文发布于:2024-02-03 06:15:16,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170691211449181.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23