【问题标题】:How do I enable geolocation support in chromedriver?如何在 chromedriver 中启用地理定位支持?
【发布时间】:2011-12-07 07:31:41
【问题描述】:

我需要使用 Selenium 测试 JS 地理定位功能,并且我正在使用 chromedriver 在最新的 Chrome 上运行测试。

现在的问题是 Chrome 在测试期间提示我启用地理定位,而我不知道如何在运行时单击那个小栏,所以我正在拼命寻找一种方法来启动 chromedriver 和 chrome 有一些选项或默认触发以启用此功能。我能找到的只是here,但是我可以完全禁用地理定位。

我该如何解决这个问题?

【问题讨论】:

    标签: selenium geolocation chromium


    【解决方案1】:

    在chromedriver wiki 的已知问题部分,他们说您无法指定自定义配置文件

    这就是为什么在我看来,@Sotomajor 回答关于在 Chrome 中使用配置文件,就像使用 firefox 一样不起作用。

    在我的一个集成测试中,我遇到了同样的问题。但是因为我不关心真正的地理位置值,所以我所要做的就是模拟 window.navigator.gelocation

    在您的 java 测试代码中放置此解决方法以避免 Chrome geoloc 权限信息栏。

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

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

    【讨论】:

    • 返回值应该是标量数字而不是字符串。
    • 只是指出这个相同的 JS 可以在 Safari/Chrome 上运行
    【解决方案2】:

    在 Firefox 中对我有用的方法是首先手动访问该站点,授予这些权限,然后将 firefox 配置文件复制到外部某处并使用该配置文件创建 selenium firefox 实例。

    所以:

    1. cp -r ~/Library/Application\ Support/Firefox/Profiles/tp3khne7.default /tmp/ff.profile

    2. 创建 FF 实例:

      FirefoxProfile firefoxProfile = new FirefoxProfile(new File("/tmp/ff.profile"));
      FirefoxDriver driver = new FirefoxDriver(firefoxProfile);
      

    我很确定类似的东西应该适用于 Chrome。虽然配置文件加载的api有点不同。你可以在这里查看:http://code.google.com/p/selenium/wiki/ChromeDriver

    【讨论】:

    • 感谢有用的提示,不是我想要的(因为我加载的 URL 的主机部分可能会随着时间的推移而改变,我必须每次更新配置文件或提供不同的配置文件),但总比没有好。
    【解决方案3】:

    这是我用水豚做黄瓜测试的方法

    Capybara.register_driver :selenium2 do |app|      
      profile = Selenium::WebDriver::Chrome::Profile.new
      profile['geolocation.default_content_setting'] = 1
    
      config = { :browser => :chrome, :profile => profile }    
      Capybara::Selenium::Driver.new(app, config)
    end
    

    还有其他有用的配置文件设置的链接:pref_names.cc

    查看RubyBindings 中的“调整配置文件首选项”

    【讨论】:

      【解决方案4】:

      至于你最初的问题:

      您应该手动启动一次 Firefox - 并选择您用于 Selenium 的配置文件。

      在地址栏中输入about:permissions;找到您的主机名称 - 并选择 share location : "allow"。

      就是这样。现在,您的 Selenium 测试用例将不会看到不在 DOM 中的可怕浏览器对话框。

      【讨论】:

        【解决方案5】:

        我采用了你的方法,但它不起作用。我在 Chrome 的首选项配置中没有找到“BaseUrls.Root.AbsoluteUri”。并使用脚本进行测试

        chromeOptions = webdriver.ChromeOptions()
            chromeOptions.add_argument("proxy-server=http://127.0.0.1:1087")
            prefs = {
                'profile.default_content_setting_values':
                    {
                        'notifications': 1,
                        'geolocation': 1
                    },
                'devtools.preferences': {
                    'emulation.geolocationOverride': "\"11.111698@-122.222954:\"",
                },
                'profile.content_settings.exceptions.geolocation':{
                    'BaseUrls.Root.AbsoluteUri': {
                        'last_modified': '13160237885099795',
                        'setting': '1'
                    }
                },
                'profile.geolocation.default_content_setting': 1
        
            }
        
            chromeOptions.add_experimental_option('prefs', prefs)
            chromedriver_path = os.path.join(BASE_PATH, 'utils/driver/chromedriver')
            log_path = os.path.join(BASE_PATH, 'utils/driver/test.log')
            self.driver = webdriver.Chrome(executable_path=chromedriver_path, chrome_options=chromeOptions, service_log_path=log_path)
        

        【讨论】:

          【解决方案6】:

          我们刚刚发现了一种不同的方法,它允许我们在 chrome(当前为 65.0.3325.181(Build officiel)(64 位))上启用地理定位,而无需模拟原生 javascript 函数。

          这个想法是授权当前站点(由BaseUrls.Root.AbsoluteUri代表)访问地理位置信息。

          public static void UseChromeDriver(string lang = null)
          {
              var options = new ChromeOptions();
              options.AddArguments(
                 "--disable-plugins",   
                 "--no-experiments", 
                 "--disk-cache-dir=null");
          
              var geolocationPref = new JObject(
                  new JProperty(
                      BaseUrls.Root.AbsoluteUri,
                      new JObject(
                          new JProperty("last_modified", "13160237885099795"),
                          new JProperty("setting", "1")
                      )
                  )
               );
          
            options.AddUserProfilePreference(
                "content_settings.exceptions.geolocation", 
                 geolocationPref);
          
              WebDriver = UseDriver<ChromeDriver>(options);
          }
          
          private static TWebDriver UseDriver<TWebDriver>(DriverOptions aDriverOptions)
              where TWebDriver : RemoteWebDriver
          {
              Guard.RequiresNotNull(aDriverOptions, nameof(UITestsContext), nameof(aDriverOptions));
          
              var webDriver = (TWebDriver)Activator.CreateInstance(typeof(TWebDriver), aDriverOptions);
              Guard.EnsuresNotNull(webDriver, nameof(UITestsContext), nameof(WebDriver));
          
              webDriver.Manage().Window.Maximize();
              webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
              webDriver.NavigateToHome();
          
              return webDriver;
          }
          

          【讨论】:

            【解决方案7】:
            ChromeOptions options = new ChromeOptions();
                HashMap<String, Integer> contentSettings = new HashMap<String, Integer>();
                HashMap<String, Object> profile = new HashMap<String, Object>();
                HashMap<String, Object> prefs = new HashMap<String, Object>();
            
                contentSettings.put("geolocation", 1);
                contentSettings.put("notifications", 2);
                profile.put("managed_default_content_settings", contentSettings);
                prefs.put("profile", profile);
                options.setExperimentalOption("prefs", prefs);
            

            【讨论】:

            • 此代码不起作用。
            【解决方案8】:

            设置 geoLocation 最简单的方法是在该 url 上导航并单击“selenium 允许定位”。这是引用的代码

             driver.navigate().to("chrome://settings/content");
                driver.switchTo().frame("settings");
                WebElement location= driver.findElement(By.xpath("//*[@name='location' and @value='allow']"));
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                ((JavascriptExecutor) driver).executeScript("arguments[0].click();", location);
                 WebElement done= driver.findElement(By.xpath(""));
            
                driver.findElement(By.xpath("//*[@id='content-settings-overlay-confirm']")).click();
            
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                driver.navigate().to("url");
            

            【讨论】:

              【解决方案9】:

              赛普拉斯:

              我遇到了同样的问题,但模拟对我不起作用。 我的情况: 应用程序使用地理位置进行登录。所以模拟它对我不起作用。

              解决方案: 我使用了下面的包并按照步骤和它对我有用。

              https://www.npmjs.com/package/cypress-visit-with-custom-geolocation

              工作示例: https://github.com/gaikwadamolraj/cypress-visit-with-custom-geolocation

              【讨论】:

                猜你喜欢
                • 2017-03-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2016-09-21
                • 2016-09-21
                • 2013-03-31
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多