0

我试图使用Open Browserdesired_capabilities修改用户代理字符串,并发现Chrome不再支持该技术。如何使用Robot Framework修改Google Chrome用户代理字符串

经过大量搜索和阅读,我发现有一个新版本的Selenium2library有一个新的关键字Create Webdriver应该解决这个问题。

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

我没有得到任何错误,没有警告,没有什么,除了没有修改用户代理字符串的完美工作的浏览器。

我试图修改其他选项,如--start-maximized,结果相同,即根本没有结果。从关键字

摘录打开谷歌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.16 50.59
  • 硒2.39.0
  • Selenium2library 1.5
  • 机器人框架2.8.3
  • 机器人框架乘坐1.2.2

那么是什么问题?

+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 
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)

0

我尝试使用RF设置用户代理铬及以下片段为我工作很好...!

${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}