如何使用Robot Framework修改Google Chrome用户代理字符串

时间:2014-01-16 07:28:19

标签: google-chrome selenium selenium-webdriver robotframework

我尝试使用Open Browser使用desired_capabilities修改用户代理字符串,并发现Chrome不再支持该技术。

经过多次搜索和阅读后,我发现有一个新版本的Selenium2library有一个新的关键字Create Webdriver,可以解决这个问题。

我修改了他们的例子来满足我的需求。但无论我做什么,它都不会修改用户代理字符串。

除了没有修改用户代理字符串的完美浏览器外,我没有错误,没有警告,没有任何内容。

我尝试使用相同的结果修改其他选项,例如--start-maximized,即根本没有结果。

摘自打开Goog​​le Chrome的关键字,并(据称)修改用户代理字符串:

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()   sys, selenium.webdriver
${options.add_argument}=    Set Variable    user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 System/ComputerId" 
Create WebDriver    Chrome  chrome_options=${options}   
Go To   http://www.useragentstring.com

在这里给出的例子(在页面底部)之后塑造:

https://github.com/rtomac/robotframework-selenium2library/issues/225

我的软件设置:

  • Google Chrome 31.0.1650.59
  • Selenium 2.39.0
  • Selenium2library 1.5
  • Robot Framework 2.8.3
  • Robot Framework Ride 1.2.2

那么问题是什么?

3 个答案:

答案 0 :(得分:2)

经过一些修补和阅读之后,我设法找到了一种方法让这个例子有效。

我没有使用${options.add_argument}=,而是使用Call Method ${options} add_argument

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
Call Method    ${options}    add_argument    --user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36 System/ComputerId
Create WebDriver    Chrome    chrome_options=${options}    
Go To    http://www.useragentstring.com

答案 1 :(得分:0)

我遇到过类似的问题。我尝试运行你的代码,但没有运气使其工作。它只是说没有定义用户代理。我浏览过并遇到过这段代码,但不幸的是用python编写:

from selenium import webdriver
import webbrowser
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
    "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
chrome_options = Options()
chrome_options.add_experimental_option("mobileEmulation", mobile_emulation)
driver = webdriver.Chrome(chrome_options = chrome_options)

答案 2 :(得分:0)

我尝试使用RF在Chrome中设置用户代理,并在下面的代码段中正常工作......!

${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
${userAgent}=  set variable  --user-agent="Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
Call Method    ${options}  add_argument  ${userAgent}
Create WebDriver    Chrome    chrome_options=${options}