解惑
${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
按照这方法写了,运行就报错
Calling method 'add_argument' failed: TypeError: add_argument() got an unexpected keyword argument '--user-agent'
我就纳闷了,为什么同样的写法就是不行呢,在网上搜了很久,终于发现了这个问题的根本所在,这样的写法本身没有问题,有问题的是 --user-agent=
这个=
等号,我们想做的是在user-agent
里面找到后面的模拟器,但是直接=,RobotFramework就会直接去查找--user-agent
而不是找模拟器
Question:怎么解决??
Answer:将--user-agent=
写成--user-agent\=
,后面跟上模拟器
亲测有效
至于如何知道自己本机chrome的user agent是什么,自行网上查找,不在这里赘述
进阶
上面只是介绍如何正确启动UA,如果你运行了会发现浏览器的尺寸根本不像手机,那如何有手机浏览器的效果呢,看代码
${device metrics}= Create Dictionary 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
${mobile emulation}= Create Dictionary deviceMetrics=${device metrics}
${chrome options}= Evaluate sys.modules['selenium.webdriver'].ChromeOptions() sys, selenium.webdriver
Call Method ${chrome options} add_experimental_option mobileEmulation ${mobile emulation}
Create Webdriver Chrome chrome_options=${chrome options}