如何使用Selenium Webdriver在Mozilla和Chrome浏览器中处理地理位置弹出窗口?

4
请看以下截图: 我的问题截图 使用Selenium WebDriver如何处理Mozilla和Chrome浏览器中的地理位置弹出窗口?
    package tiyotesting;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.firefox.internal.ProfilesIni;
    import org.openqa.selenium.support.ui.Select;
    public class Citydropdownlist {
        public static void main(String[] args) throws InterruptedException {
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.google.com");
            driver.get("http://ec2-35-154-164-82.ap-south-1.compute.amazonaws.com/tiyorelease3/");
            WebElement ListBox = driver.findElement(By.id("supported_city_label"));
            ListBox.sendKeys("Ahmedabad");
            ListBox.sendKeys(Keys.ENTER);
        }
    }

我创建了一个Firefox自定义配置文件,但它仍然无法正常工作,弹出窗口再次出现,这是我的噩梦,请帮助我解决这个问题。


我创建了Firefox自定义配置文件,但它仍然无法正常工作,弹出窗口再次出现,这对我来说是一个致命问题,请帮助我解决这个问题。 - Premkumar Yadav
我无法在google.com中看到地理位置弹出窗口 - 它会自动重定向。请考虑提供弹出窗口的截图。 - iamkenos
我需要在哪里附加文件? - Premkumar Yadav
请编辑您的问题并附上图像。 - iamkenos
我已经编辑并附上了,请查看。 - Premkumar Yadav
2个回答

4

在使用Selenium 3.x,geckodriver v0.16.1和Mozilla Firefox 53.x时,您可以通过以下方式在新的Firefox配置文件中设置首选项来禁用地理位置弹窗:

  1. You have to download the geckodriver.exe from here. Save it on your machine.
  2. You have to mention the absolute path of the geckodriver.exe through System.setProperty
  3. You don't require to do driver.get("http://www.google.com"); to open any other URL.
  4. Here is the working set of minimal code which opens the intended URL without the Geo Location popup.

    System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
    FirefoxProfile geoDisabled = new FirefoxProfile();
    geoDisabled.setPreference("geo.enabled", false);
    geoDisabled.setPreference("geo.provider.use_corelocation", false);
    geoDisabled.setPreference("geo.prompt.testing", false);
    geoDisabled.setPreference("geo.prompt.testing.allow", false);
    WebDriver driver=new FirefoxDriver(geoDisabled); 
    driver.get("http://ec2-35-154-164-82.ap-south-1.compute.amazonaws.com/tiyorelease3/"); 
    

先生,我有一个关于同一段代码的问题...我该如何在列表框中选择项目?请帮帮我@DebanjanB - Premkumar Yadav
感谢您的建议,DebanjanB先生。 - Premkumar Yadav

3

使用FirefoxProfile对象进行Firefox驱动程序的初始化已经被弃用。我使用了相同的偏好设置,取而代之。这对我起作用了。

File gecko = new File("C:\\geckodriver\\geckodriver.exe");

FirefoxOptions ffopt = new FirefoxOptions()
    .addPreference("dom.webnotifications.enabled", false)
    .addPreference("geo.enabled", false)
    .addPreference("geo.provider.use_corelocation", false)
    .addPreference("geo.prompt.testing", false)
    .addPreference("geo.prompt.testing.allow", false);


System.setProperty("webdriver.gecko.driver", gecko.getAbsolutePath());
WebDriver driver = new FirefoxDriver(ffopt);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接