How to ignore geo location popup using java selenium - java

After allowing for "all the site to get your physical location" using chrome setting, still popup getting populated. How to handle geo location popup using java selenium? Can anyone please help me?

To disable this message you need to add chrome options:
options.addArguments("--enable-strict-powerful-feature-restrictions");
options.addArguments("--disable-geolocation");
Full example:
public WebDriver createDriver(){
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("disable-notifications");
options.addArguments("--enable-strict-powerful-feature-restrictions");
options.addArguments("--disable-geolocation");
return new ChromeDriver(options);
}

Related

Selenium can't open Gitlab login page

I'm doing an automated test, using Java (8), Selenium (4) and Chromedriver (98.0).
The test is for a site that requires to login with different third party accounts, one of them being GitLab.Unfortunately the test always gets stuck while trying to access Gitlab's login page, on the "Checking your browser before accessing gitlab.com" part. If I pause the test on this step and duplicate the tab manually, the newly opened tab will be able to enter and then the first one will be also able to do it (probably because at that point finally has a valid cookie).I've tried out different solutions but with no luck. Currently this is my code:
#Test
public void test() throws MalformedURLException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-dev-shm-usage");
options.addArguments("--disable-blink-features=AutomationControlled");
options.addArguments("--start-maximized");
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
options.setExperimentalOption("useAutomationExtension", false);
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
driver.navigate().to("https://gitlab.com/users/sign_in");
new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(60))
.pollingEvery(Duration.ofSeconds(2))
.until(x -> driver.findElements(By.xpath("//*[#data-translate='checking_browser']")).size() == 0);
}
When trying to use undetected_chromedriver, using Python, it worked, but it's a requirement for me to use Java. Is there something similar for Java or is there an extra ChromeOption that I'm missing?
need to download chrome driver https://chromedriver.chromium.org/downloadshttps://chromedriver.chromium.org/downloads
and add this code while setting up driver
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();

How to accept personal certificate using selenium java

We are trying to load a client URL and it is must to accept the personal certificate installed in my local machine. I am using robot keys to click on 'OK' button on the certificate popup. By the time it is clicking on 'Ok' button i am experiencing session timeout and the script is failing. I have also tried to reduce the implicit time.
Is there a way to solve the issue by setting chrome capabilities to select the personal certificate based on the name of the certificate(I have Multiple certificates based on the user) during the driver initialization.
For accepting alert you could use below code :
DesiredCapabilities caps = DesiredCapabilities.chrome ()
caps.setCapability (CapabilityType.ACCEPT_SSL_CERTS, true)
WebDriver driver = new ChromeDriver (caps);
You can try something like this at driver initialization level.
Code :
ChromeOptions options = new ChromeOptions();
options.setCapability(capabilityName, value);
//options.setAcceptInsecureCerts(acceptInsecureCerts)
WebDriver driver = new ChromeDriver(options);

How to close a chrome browser pop up window?

I am writing a code for Facebook where it takes the URL, ID, Password from a properties file but upon logging in I am hinted with a "Facebook wants to show notifications - Allow - Block" How do I make it so after login it (A.) Presses ESP or ALT+F4 and closes the popup or (B.) Finds the notification and closes it itself. This is what Im using but its not working. Any help is appreciated.
public void closePopup() throws InterruptedException{
Thread.sleep(1000);
Actions action=new Actions(driver);
action.keyDown(Keys.ESCAPE).keyUp(Keys.ESCAPE).build().perform();
After further research I found my answer. It is a chrome notification so here is the required step to solve my problem.
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
driver = new ChromeDriver(ops);
Please Follow below steps :
Step 1:
//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();
Step 2:
//Add chrome switch to disable notification - "--disable-notifications"
options.addArguments("--disable-notifications");
Step 3:
//Set path for driver exe
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
Step 4 :
//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);

selenium get() doesn't do anything

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
The code below should open a firefox browser, navigate to google and search for "automation" and should navigate again to yahoo. But driver.get("http://www.yahoo.com") doesn't do anything. How will I change URL using selenium?
driver.get("http://www.google.com");
widget=driver.findElement(By.id("lst-ib"));
widget.click();
widget.sendKeys("automation");
widget.sendKeys(Keys.ENTER);
driver.get("http://www.yahoo.com");
widget=driver.findElement(By.xpath(".//*[#id='yui_3_12_0_1_1452245228407_940']/td[1]/a"));
widget.click();
The same is working fine for me after pausing/stopping the my kaspersky internet security.
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("seleniumhq");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
Thread.sleep(6000);
driver.get("http://www.yahoo.com");
Assuming you are running code on Windows, can you check your hosts file entry. You can find it in below location:
C:\Windows\System32\Drivers\etc\hosts
Have a look at the file content and check if localhost resolution has been altered.
If it does not help, post selenium version you are using, error message which you are getting.
You cannot open in same object. Two Options
Option 01:
You can open in new window like this
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
WebElement widget = driver.findElement(By.id("lst-ib"));
widget.click();
widget.sendKeys("automation");
widget.sendKeys(Keys.ENTER);
WebDriver seconddriver = new FirefoxDriver();
seconddriver.get("http://www.yahoo.com");
Option 02:
you can use keyboard keys to open new window or tab and then using driver.switchto.

How to turn off the proxy settings on chrome browser through selenium webdriver?

When I navigate to my url on chrome, I get The system cannot find the file specified." . I thought it might be due to automatic proxy settings on chrome.
I want to explicitly turn off the proxy setting before starting chrome browser in selenium. I tried below, it isn't working. Can anyone help me
ChromeOptions options = new ChromeOptions();
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability("chrome.setProxyByServer", false);
System.setProperty("webdriver.chrome.driver",sChromeDriverPath);
WebDriver driver = new ChromeDriver();
No errors are thrown at any point of time but URL doesn't open up
Tia
Anjana
You need to pass the options object to the chrome driver when you initialize it. If you use a specific capability then pass it to the chromeDriver(), so that chrome knows what to start with. Also there is no JSON object as setProxyByServer in chrome, instead use noProxy JSON object. Check this out. Here's how -
Proxy proxy=startProxy();
proxy.setProxyType(ProxyType.MANUAL);
proxy.setNoProxy("");
ChromeOptions options = new ChromeOptions();
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(CapabilityType.PROXY, proxy);
System.setProperty("webdriver.chrome.driver",sChromeDriverPath);
dc.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(dc);
More info about chrome capabilities. Hope it helps.

Categories

Resources