DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("--start-maximized", "allow-running- insecure-content", "ignore-certificate-errors")); capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver webDriver = new RemoteWebDriver(new URL("http://www.google.com"), capabilities);
webDriver.findElement.....
I have the above piece of code to start a browser and go to a URL. I did a
1. brew install chromedriver
2. i made sure /usr/local/bin is in the path variable.
3. When i run the above peice of code it fails in find Element giving a null pointer exception.
I am running mac os.. how can i fix the problem. i did lot of work arounds, the same code without capabilities and just giving the chromedriver path was working before.. not sure why i am not able to get it to run..
There are 2 solutions to your problem:
Make sure you want to use RemoteWebDriver. If you do you will want to set up a Selenium Grid with a chrome node. I won't describe how to set it up here, but it is fairly easy to set up a local grid+nodes using docker: https://github.com/SeleniumHQ/docker-selenium
If you do use this, you will need to change the driver to (assuming the grid is on the localhost):
String hubURL = "http://localhost:4444/wd/hub";
WebDriver webDriver = RemoteWebDriver(new URL(hubURL), capabilities);
Or Use the ChromeDriver class instead of RemoteWebDriver:
WebDriver webDriver = new ChromeDriver(capabilities);
Finally, to go to a webpage, you'll need to use the get method on the driver:
webDriver.get(url);
Updating to the latest chrome driver fixed the issue.
Related
I want test the IE mode for Edge browser with Selenium. I found the solution on the MS site here:
https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/ie-mode?tabs=java
I am using the following code as given in the above link:
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.attachToEdgeChrome();
ieOptions.withEdgeExecutablePath("C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe");
WebDriver driver = new InternetExplorerDriver(ieOptions);
I can get the error that the methods "attachToEdgeChrome()" and "withEdgeExecutablePath()" are not defined in the InternetExplorerOptions. Is there anything I am missing here?
Note: My selenium jar is selenium-server-4.0.0-alpha-2.jar
As per the ChangeLogs 0f Selenium v4.0.0.0-alpha-2:
Add Chromium-based Edge support. This involves adding a new Chromium driver to the tree too.
So ideally, the code block from the documentation Use Internet Explorer Driver to automate IE mode in Microsoft Edge should have worked seamlessly.
However, as per best practices instead of using the alpha and beta releases, you should always prefer the GA releases to execute your tests and you can pickup anyone from the following options:
Selenium v4.1.3
Selenium v4.1.2
Selenium v4.1.1
Selenium v4.1.0
Selenium v4.0.0
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.http", "localhost");
profile.setPreference("network.proxy.http_port", 3128);
WebDriver driver = new FirefoxDriver(firefox_profile=profile);
I am using below code to config but it's giving an error. You should need to be include executive path. How can I solve it. I am Running this code in pycharm.
You can also see the image for more brief
You need to specify the path.
Try this out option 1
option 2
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/path/to/geckodriver')
browser.get('https://www.google.com')
I was getting the error:
The path to driver executable must be set by the webdriver.gecko.driver system property
Then i downloaded geckodriver and set the path as below. After that the code is working fine.
But my question is do i need to use the below code every time (in each and every program) when i want to launch a firefox using selenium code?
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver();
When we work with Selenium 3.x, geckodriver and Mozilla Firefox Browser through Selenium-Java bindings we need to configure the Test Environment through System.setProperty line. Find the details along with your Answer below.
Your Question have 2 parts so I will answer both of them in parts:
1. Do I need to use the below code every time in each and every Program:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
WebDriver driver = new FirefoxDriver();
Answer:
Yes
Explaination:
Whenever we need to execute a program (Selenium-Java based) it is mandatory we explicitly mention the type of driver (gecko, chrome, ie) which we are trying to use in our program in the form of "webdriver.gecko.driver". Along with it, we also need to explicitly mention the absolute path of the driver (gecko, chrome, ie) binary (.exe) in the form of "<path to geckodriver.exe>". Next we are using the WebDriver interface and casting the WebDriver instance to FirefoxDriver.
2. Do I need to use the below code every time when i want to launch Firefox:
System.setProperty("webdriver.gecko.driver","<path to geckodriver.exe>");
Answer:
No
Explaination:
Once we configure the WebDriver instance i.e. the driver through DesiredCapabilities class, the driver is able to carry the same configuration till its life time which is controled through your Automation Script. So until and unless we explicitly call quit() method through the driver, the driver instance remains active and carries the configuration. So, within your program no matter how many time you choose to close the browser instance by calling close() method, you can always mention driver = new FirefoxDriver(); to open a new browser session again and again with the stored configuration within the driver.
An Example:
package demo;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Driver_Close_Initiate
{
static WebDriver driver;
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability("marionette", true);
driver = new FirefoxDriver(dc);
driver.get("https://google.com");
driver.close();
driver = new FirefoxDriver(dc);
driver.get("https://facebook.com");
driver.quit();
}
}
The reason you need to use the gecko driver is your Firefox version is greater than v47. So to answer your question, if you don't want to use the gecko driver everytime, you need to downgrade your Firefox version to 47 or below.
Previous versions can be found here:
https://ftp.mozilla.org/pub/firefox/releases/
Yes you have to use setProperty(String key, String path) every time if you want to use Firefox version above 47 and selenium jars above 3.0.
System.setProperty("webdriver.gecko.driver", "Path of geckodriver.exe");
or, you can set the configurtion path using DesiredCapabilities classs
DesiredCapabilities des_capablity = DesiredCapabilities.firefox();
des_capablity.setCapability("marionette", true);
driver = new FirefoxDriver(des_capablity);
I'm trying start selenium tests in chrome browser with my custom Profile which contains necessary cookies.
I use Chrome 57 and chromedriver 2.29
My code is :
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches", "--disable-extensions");
options.addArguments("user-data-dir=/Users/tester/Desktop/ChromeProf/QAChromeProfile");
options.addArguments("--ignore-certificate-errors","disable-infobars","enable-automation","--window-size=375,667");
WebDriver driver = new ChromeDriver(options);
It works fine, but don't use my chrome profile. Help me, pls...)
Don't include the profile directory in the user-data-dir argument. When using user-data-dir option with any profile besides the default, i've had to also use the profile-dir argument.
Make sure you change user-data-dir to your Chrome User Data directory. Don't worry about spaces. Do not try to put quotes or to escape spaces in the argument value.
Profile 1
from selenium.webdriver import Chrome, ChromeOptions
options = ChromeOptions()
options.add_argument("user-data-dir=C:/Users/<username>/AppData/Local/Google/Chrome/User Data")
options.add_argument("profile-directory=Profile 1")
driver = Chrome(executable_path=r'C:/path/to/chromedriver.exe', chrome_options=options)
driver.get("https://www.google.com")
Keep this in mind when running your code. (basically, close any open Chrome instances running before running selenium with custom Chrome profiles in use)
I tried to load chrome profile using selenium weDriver. The profile loads fine but it failed when it tries to load the URL.
I noticed that this issue happens when there is another chrome instance open whether or not it was open by webDriver. I have selenium 2.53.1.
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/useName/AppData/Local/Google/Chrome/User Data");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
driver.get("www.google.com") // here is where it fails. It works fine if I close all chrome browsers before I run the test
I found a workaround for this issue. I noticed that this issue happens because chromedriver will not be able to launch with the same profile if there is another open instance using the same profile. For example, if chrome.exe is already open with the default profile, chromedriver.exe will not be able to launch the default profile because chrome.exe is already open and using the same profile.
To fix this, you will need to create a separate profile for automation by copying the default profile so that chromedriver.exe and chrome.exe don't share the same default profile.
The default chrome profile is in this location:
C:\Users\yourUserName\AppData\Local\Google\Chrome\User Data\
Copy all files from User Data folder to a new folder and call it AutomationProfile
After you copy the files to the new folder then you can use it for your scripts.
String userProfile= "C:\\Users\\YourUserName\\AppData\\Local\\Google\\Chrome\\AutomationProfile\\";
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir="+userProfile);
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
Make sure you use driver.quit() at the end of your test so that you don't keep chromedriver.exe open
I added the ChromeOption "no-sandbox", and it seemed to help me with a similar issue. Know that this changes how secure your browsing can be. Here's a link that explains it more: https://www.google.com/googlebooks/chrome/med_26.html
var options = new ChromeOptions();
//I had more options added, but this is the example of the argument I referred to
options.AddArgument("no-sandbox");