如何在Selenium的chromedriver中启用地理位置支持? [英] How do I enable geolocation support in chromedriver for Selenium?

查看:926
本文介绍了如何在Selenium的chromedriver中启用地理位置支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用Selenium测试JS地理定位功能,并且使用chromedriver在最新的Chrome上运行测试。现在的问题是,Chrome会提示我在测试过程中启用Geolocation,并且我不知道如何在运行时点击那个小栏,所以我正在拼命寻找一种启动chromedriver和chrome的方法,并带有一些选项或触发器来默认启用它。我可以在这里找到我所能完全禁用地理位置的信息。



我该如何解决这个问题? 解决方案

chromedriver wiki ,他们表示您无法指定自定义配置文件 p>

这就是为什么在我看来,@Sotomajor回答关于使用Chrome浏览器的个人资料,就像您使用Firefox所做的一样,将无法正常工作。



在我的一个集成测试中,我遇到了同样的问题。但是因为我并不打扰真正的地理定位值,我所要做的就是模拟window.navigator.gelocation。在您的java测试代码中,为避免这种情况, Chrome geoloc权限信息栏。

 
chromeDriver.executeScript(window.navigator.geolocation.getCurrentPosition =
function(success){
var position = {coords:{
latitude:555,
longitude:999
}
};
成功(位置);});

这里的纬度(555)和经度(999)值只是测试值

I need to test a JS geolocation functionality with Selenium and I am using chromedriver to run the test on the latest Chrome.

The problem is now that Chrome prompts me to enable Geolocation during the test and that I don't know how to click that little bar on runtime, so I am desperately looking for a way to start the chromedriver and chrome with some option or trigger to enable this by default. All I could find here was however how I can disable geolocation altogether.

How can I solve this issue?

解决方案

In the known issues section of the chromedriver wiki they said that you Cannot specify a custom profile

This is why it seems to me that @Sotomajor answer about using profile with Chrome as you would do with firefox will not work.

In one of my integration tests I faced the same issue. But because I was not bothering about the real geolocation values, all I had to do is to mock window.navigator.gelocation

In you java test code put this workaround to avoid Chrome geoloc permission info bar.

chromeDriver.executeScript("window.navigator.geolocation.getCurrentPosition = 
    function(success){
         var position = {"coords" : {
                                       "latitude": "555", 
                                       "longitude": "999"
                                     }
                         }; 
         success(position);}");

latitude (555) and longitude (999) values here are just test value

这篇关于如何在Selenium的chromedriver中启用地理位置支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆