How to open safari browser using Selenium WebDriver? - selenium-webdriver

Can anyone please explain me the below doubt?
How to open google page in safari browser using Selenium WebDriver?
I am using the below code to open the safari browser.
WebDriver driver = new SafariDriver();
driver.get("http://www.google.com/");
But I am getting "SafariDriver requesting connection at ws://localhost:40921" error message
Can anyone please help me for the above?

You need to install Safari Extensions Developer Certificate in your machine and also install selenium webdriver as an extension in Safari Browser.
Also there are some stability issues with Safari browser & Selenium combination on Mac 10.9 OS and below.
The stability is much better on Mac Yosemite OSX & Safari 8.0.x combination along with Selenium v2.45.0

Have you checked out these links?
http://code.google.com/p/selenium/wiki/SafariDriver
http://code.google.com/p/selenium/issues/detail?id=3827

Prerequisite: Install Safari on Windows if it is not already installed
Go to http://docs.seleniumhq.org/download/
Scroll down -> Go to the section "SafariDriver" and download "SafariDriver.safariextz"
Double click on "SafariDriver.safariextz" (previously downloaded)
Safari would open with a pop up containing "Install" button -> Click Install button
Now go to Preferences of Safari -> Extensions tab : you would see WebDriver (in my case WebDriver 2.48.0) is installed (Enable WebDriver checkbox is checked))
It's now time to instantiate SafariDriver and get the desired URL by using Java. Please find the code:
WebDriver driver = new SafariDriver();<br>
driver.get("https://www.lynda.com/Ripon-Al-Wasim/8496323-1.html");

First of all, you don't need to download safaridriver if you having MacOS High Sierra and later, as its already present by default at
cd /usr/bin
ls -a
Can check here safaridriver is already present
so how to configure this safaridriver in test files
private static final String KEY_SAFARI_WEBDRIVER = "webdriver.safari.driver";
private static final String SAFARI_WEBDRIVER_PATH = "/usr/bin/safaridriver";
System.setProperty(KEY_SAFARI_WEBDRIVER, SAFARI_WEBDRIVER_PATH);
WebDriver driver=new SafariDriver();
By default automation testing option is disabled in safari,
can enable it from safari browser
Develop -> Allow remote automation
reference: https://www.lambdatest.com/blog/selenium-safaridriver-macos/

Try this instead:
System.setProperty("webdriver.safari.driver","C:/safaridriver.exe")
WebDriver driver = new SafariDriver();

Related

selenium browser launch by octopus [duplicate]

The release notes for ChromeDriver 2.33 says that ""Fixes a bug which caused Resizing/Positioning Window commands to fail on Chrome 62+" however this still seems to be an issue when i am using Chrome 62+ browser. Maximizing chrome window using chrome driver results in below exception. Does anyone know a solution please?
Another thing i noticed is, though i installed latest chromedriver (v2.33) from https://chromedriver.storage.googleapis.com/index.html?path=2.33/, the log printed below says Driver info: chromedriver=2.25.426923 !!
Exception in thread "main" org.openqa.selenium.WebDriverException:
unknown error: cannot get automation extension from unknown error:
page could not be found:
chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
(Session info: chrome=62.0.3202.62) (Driver info:
chromedriver=2.25.426923
(0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT
10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
There are exactly 2 issues.
As you mentioned, you have installed latest chromedriver (v2.33) but the log printed below says Driver info: chromedriver=2.25.426923, this issue must be addressed first. You can consider to manually kill all the dangling chromedriver.exe tasks from the Task Manager. Additionally you can consider to use CCleaner to wipe out all the rotten OS stuffs from your system. Take a system reboot if required. Finally ensure that what ever the absolute location of chromedriver.exe you are using within System.setProperty() ensure that the chromedriver binary is of version 2.33.
Finally, it is suggested to use ChromeOptions class to maximize the Web Browser as follows:
System.setProperty("webdriver.chrome.driver", "C:\\your_directory\\chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("disable-infobars");
opt.addArguments("--start-maximized");
opt.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(opt);
driver.get("https://google.com");
Here are some of the alternatives which may solve your question:
Using maximize() from WebDriver.Window interface :
driver.manage().window().maximize();
Using setSize(Dimension targetSize) from WebDriver.Window interface:
driver.manage().window().setSize(new Dimension(800, 600));
Using addArguments("--start-maximized") through ChromeOptions:
chromeOptions.addArguments("--start-maximized");
Using addArguments("--window-size=1920,1080") through ChromeOptions:
chromeOptions.addArguments("--window-size=1920,1080");
Using executeScript() from JavaScriptExecutor interface:
((JavaScriptExecutor)driver).executeScript("window.resizeTo(1024, 768);");
You can find a related discussion in Chrome - org.openqa.selenium.WebDriverException: unknown error: cannot get automation extension at driver.manage().window().maximize();.
I believe there were some old chrome driver processes running in backend and same were being picked up when it was invoked via code. I deleted all processes instances, deleted old version of chrome driver, added the new 2.33 version and it worked. Thanks all for your suggestions.
I think the reason behind it may be your chrome version. Try again with updating your chrome browser. I have faced this type of issues for compatibility between chrome browser & the driver
Use class ChromeOptions.
System.setProperty("webdriver.chrome.driver", "h:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get(url);

Firefox Webdriver fails with UnreachableBrowserException and blank Screen

I am running webdriver 2.53.1 against firefox 45.9.0ESR on Redhat Linux 6.6. FirefoxDriver object gets created successfully and firefox launches with blank page (about:blank) as expected. But when I do the 'get' to open the url, it fails with UnreachableBrowserException with underlying cause as org.apache.http.NoHttpResponseException: localhost:7055 failed to respond.
Preferences set for FirefoxProfile
app.update.auto = false
app.update.enabled = false
app.update.silent = false
media.gmp-provider.enabled = false
webdriver.log.file = webdriver_debug.log
webdriver.firefox.logfile = firefox_browser.log
My observations
Browser is running and is not killed
Browser has webdriver addon added.
By 'netstat' I see webdriver listening on port 7055
Though I configured to dump firefox and webdriver logs, nothing gets dumped.
What I tried so far
Handling the exception and retrying does not help
The firefox is a tar ball extract. I tried removing the folder and extracting again, but that did not help either.
Used navigate().to(url) instead of get(url) but result is same.
NOTE: The JRE 7 is used for running
Upgrading Selenium
We cannot upgrade the selenium or firefox as there are many other dependent layers to be upgraded for selenium/firefox to upgrade.
You need to update your jars files of selenium both server and client.
Download the latest jars from below link :-
http://www.seleniumhq.org/download/
You also need to update your gecko driver from below URL :-
https://github.com/mozilla/geckodriver/releases
Additionality update your firefox
Help -> About

Which version of firefox is compatible with Selenium 3.3.1 without use of gecodriver

I want to run scripts on Firefox having selenium webdriver version installed as 3.3.1 but w/o any use of geco driver.
kindly help as I am working on client machine
No, you cannot work with Selenium 3.3.1 without using geckodriver.
Selenium 2.53 was the last version which worked without gecko driver with Mozila Firefox (till version 47.x)
To work with Selenium 3.3.1 you need to mandatory download gecko driver v0.15.0 from here and save it. Upgrade your Mozila Firefox to 53.x
Note that: For gecko driver v0.15.0 you must upgrade to Selenium 3.3 since this release aligns HTTP responses with the WebDriver standard.
Next you need to provide the absolute path of the gecko driver in your code. Your code will look like:
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com/");
driver.findElement(By.name("email")).sendKeys("your_username");
Let me know if this answers your question.

Launch chrome browser

I am trying to launch chrome browser (version 26.0) using webdriver.
I am getting the following error message.
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver.
at com.google.common.base.Preconditions.checkState(Preconditions.java:176)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:105)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:69)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:107)
at googleSearch.main(googleSearch.java:13)
Code I used:
driver = new ChromeDriver();
driver.navigate().to("http://www.google.com/");
I use mac 10.8.2.
For this to work, you need to:
Install Chrome
Install Chrome Web Driver
Make sure you have the chrome web driver in you path, for example on Windows something pointing to chromedriver2_win32_0.8. You can put that in your path by either: (a) Modifying your windows path environment variable, or; (b) adding the following to your java command line options:
-Dwebdriver.chrome.driver='/path/to/driver'
In case of using selenium grid
-Dwebdriver.chrome.driver='/path/to/driver'
has to be added while creating a node from command line.
1) In case of using selenium without GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
does the job.
2) In case of using selenium with GRID:
System.setProperty("webdriver.chrome.driver","/absolute/path/to/chromedriver");
driver = new ChromeDriver();
And from command line, while creating a node for chrome browser one needs to pass
-Dwebdriver.chrome.driver='/absolute/path/to/chromedriver'
The above two changes did the job for me, apart from this I was getting this libnss3.so not found error which I solved by creating a symlink of libnss3.so present in /usr/lib/x86_64-linux-gnu/ folder to /usr/lib/
ln -s /usr/lib/x86_64-linux-gnu/libnss3.so /usr/lib/libnss3.so
PS: Also make sure that you are using 64bit OR 32bit version of chrome driver as per your system.
For chrome to work with selenium-webdriver you need to have not only a working chrome browser installed, but also the chromedriver executable. Note that these are TWO different executable files that both need to be specified.
change the permission of file and then run your code again.
Open command prompt and navigate to directory where your chrome exe exists and write
chmod 777 filename
Hope it will solve your problem.

How to handle Windows Download dialog box using selenium web driver

I am using selenium web driver (Not selenium RC). I need to download a xml file by clicking a link. I have did some Google search, and I have found in some answers an using AutoIT in order to handle OS related dialog boxes.
But is there any other option using selenium to handle this without using AutoIT tool.
Kindly suggest some ideas.
Can you be a bit more specific which browser you are using. If it's firefox you can have a greater control on the file download. Any other browser including firefox you can use robot class. This can be used to perform the click on ok button for downloads. If its chorme then file download happens automatically without any intervention.
For the newest version of Firefox (as of writing), these are the arguments that I needed to avoid the download box. Note that you need to specify a directory that you can write to as seen in the third statement:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "browser.download.folderList", 2 );
profile.setPreference( "browser.download.dir", <YOUR DOWNLOAD PATH> );
profile.setPreference( "plugin.disable_full_page_plugin_for_types", "application/pdf" );
profile.setPreference(
"browser.helperApps.neverAsk.saveToDisk",
"application/csv,text/csv,application/pdfss, application/excel" );
profile.setPreference( "browser.download.manager.showWhenStarting", false );
profile.setPreference( "pdfjs.disabled", true );
Note that the last line with pdfjs is required for the newer versions of Firefox, where it wasn't before. More info Here
I face Authentication Proxy pop-up issue in my project. So I tried below solution and it is working fine.
When we run Script from Selenium Web driver on Security Environment following Setup needs to be done to handle Authentication Proxy.
First you need to know below details,
network.proxy.autoconfig_url (Example: "http://example.com/abc.pac")
network.proxy.http (Example: abc-proxy.com)
network.proxy.http_port (Example: 8080)
private static WebDriver initFirefoxDriver(String appURL)
{
System.out.println("Launching Firefox browser..");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("network.proxy.type", 1);
firefoxProfile.setPreference("network.proxy.autoconfig_url", "http://example.com/abc.pac");
firefoxProfile.setPreference("network.proxy.http", " abc-proxy.com");
firefoxProfile.setPreference("network.proxy.http_port", 8080);
WebDriver driver = new FirefoxDriver(firefoxProfile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.navigate().to(appURL);
//driver.get(appURL);
return driver;
}

Resources