使用Chrome网站驱动程序网站认为我是移动版

时间:2017-01-26 11:39:07

标签: selenium-webdriver automated-tests

我在测试用例中发生了一些奇怪的事情。

当我在IDE中进行测试时,一切正常,但在使用WebDriver时,页面上找不到元素。我通过WebDriver设置截图,屏幕截图看起来像移动视图(这与桌面视图没有相同的元素)。我认为Selenium Chrome页面应该呈现为桌面Chrome视图

我使用以下命令设置驱动程序

self.driver = webdriver.Chrome()

作为测试我在我的测试用例中有这个

driver.execute_script("return navigator.userAgent")

返回

  

Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)   Chrome / 54.0.2840.100 Safari / 537.36

哪个看起来正确,当测试用例找不到元素时,我看到了这个

  

(会话信息:chrome = 54.0.2840.100)(驱动程序信息:   chromedriver = 2.25.426924   (649f9b868f6783ec9de71c123212b908bf3b232e),平台= Linux的   4.4.0-59-通用x86_64)

再次看起来正确

所以,我的问题是,我是否应该采取其他措施来强制网站使用桌面视图。

1 个答案:

答案 0 :(得分:0)

可能是您的驱动程序窗口太小,因此网站正在加载基于此的移动视图,您可以尝试:

# replacing the dimensions below with whatever you require
driver.set_window_size(1000, 600)

$ ssh qa@192.168.19.82 'gem install iStats'
Warning: Permanently added '192.168.19.82' (ECDSA) to the list of known hosts.
ERROR:  While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.

$ ssh qa@192.168.19.82
Warning: Permanently added '192.168.19.82' (ECDSA) to the list of known hosts.
Last login: Thu Jan 26 10:50:17 2017 from 192.168.21.168
qa$ gem install iStats
Building native extensions.  This could take a while...
Successfully installed iStats-1.4.0
Parsing documentation for iStats-1.4.0
Done installing documentation for iStats after 0 seconds
1 gem installed

qa$ id
uid=502(qa) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),33(_appstore),100(_lpoperator),204(_developer),395(com.apple.access_ftp),101(com.apple.access_screensharing-disabled),102(com.apple.access_ssh-disabled)
qa$ logout
Connection to 192.168.19.82 closed.

$ ssh qa@192.168.19.82 'id'
Warning: Permanently added '192.168.19.82' (ECDSA) to the list of known hosts.
uid=502(qa) gid=20(staff) groups=20(staff),12(everyone),61(localaccounts),79(_appserverusr),80(admin),81(_appserveradm),98(_lpadmin),33(_appstore),100(_lpoperator),204(_developer),395(com.apple.access_ftp),101(com.apple.access_screensharing-disabled),102(com.apple.access_ssh-disabled)