Selenium Chromedriver causes Chrome to start without configured plugins, bookmarks and other settings - selenium

I am a new user of Selenium. I want to use it to start up the Chrome browser but I have a problem.
public static void processor(String url, String name) {
System.setProperty("webdriver.chrome.driver", "C:/Documents and Settings/jingxiong/Local Settings/Application Data/Google/Chrome/Application/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(url);
WebElement element = driver.findElement(By.name(name));
element.sendKeys("google");
element.submit();
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
When I run this example the Chrome browser starts ok but without configured plugins, my settings or bookmarks. What should I do to cause it load these?
Thank you.

You should first read chromedriver document in selenium wiki. Its available here - http://code.google.com/p/selenium/wiki/ChromeDriver
As mentioned in the wiki:-
Similarly, to load an extension when Chrome starts:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--load-extension=/path/to/extension/directory"));
WebDriver driver = new ChromeDriver(capabilities);

Related

Selenium doesn't call web page in Chrome

I'm using Chrome browser version 57.0.2987.110 and I'm trying to open up a web page with Selenium, only thing is it's only opening the browser with the standard website but is not opening the web page I told it to. It's not an option to change the browser, I have to work with chrome.
I get this error right here:
Request for unknown Channel-associated interface: ui::mojom::GpuMain
I tried this solution so far, but it didn't work for me: Strange error in selenium after upgrading to chromedriver 2.28 needed for chrome 57
This is my code:
public static void main(String[] args) {
WebDriver driver;
String PROXY;
Proxy proxy;
proxy = new Proxy();
PROXY = "proxy.myproxy:8080";
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-gpu");
System.setProperty("webdriver.chrome.driver", "/usr/bin/google-chrome");
proxy.setHttpProxy(PROXY).setFtpProxy(PROXY).setSslProxy(PROXY);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, proxy);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.get("www.facebook.com");
}
I've just been starting with selenium and just wrote down everything really quick to try it out in main class. I know it's not pretty. Please bear with me. What am I doing wrong?
You need to update your both selenium jars and update your chrome browser as well

Not able to open Chrome headless from Selenium

I am using maven here.Here is my Selenium code:
DesiredCapabilities capb = DesiredCapabilities.chrome();
capb.setCapability("chrome.binary","/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-gpu", "--no-sandbox","--remote-debugging-port=9222");
capb.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");
try{
ChromeDriver driver = new ChromeDriver(capb);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://qa.cmnetwork.co");
driver.quit();
}
catch(Exception e)
{
e.printStackTrace();
}
when I run "mvn test" it starts the chrome in GUI mode. but It should open in Headless mode. I have chrome vesrion 59.0, OS X yosemite(10.10.5), chromedriver 2.30 and Selenium 3.4.0.
It won't open in GUI mode. Just the chrome launcher icon will be opened. And it is an expected behaviour.
You have to remove the argument --remote-debugging-port. That will block the launched headless Chrome. So the script will never move forward.And you will get a chrome not reachable error
So change the arguments like
options.addArguments("--headless","--disable-gpu", "--no-sandbox");
Also, there is no need for --no-sandbox. As per Official doc only --headless and --disable-gpu flags are enough
Unless you have multiple versions of chrome installed, there is no need for DesiredCapabilities as well.
So the simple code for headless-chrome
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless","--disable-gpu");
System.setProperty("webdriver.chrome.driver","/Users/nitinkumar/TEST/chromedriver");
ChromeDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get("https://qa.cmnetwork.co");
driver.quit();

How to run selenium code

I have the code written for selenium, but I do not know how to make selenium read the code. I know that you have to put it in a certain folder somewhere though. I have never used this program or anything like it before. I just need to know how to make selenium read my code. I have no idea what I'm doing here.
public class GoogleTest {
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
selenium.open("google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
}
You need to write the following line before creating the driver object:
System.setProperty("webdriver.chrome.driver", "C:/path/to/chrome/driver/chrome.exe");
Try following code in same order:
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
WebDriver driver = new ChromeDriver();
String baseUrl = "google.com";;
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
.
.
You may need to download the chrome driver as well.
link: https://code.google.com/p/selenium/wiki/ChromeDriver

Browser Plugin Testing With Selenium

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.
Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?
I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.
The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.
The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.
The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:
Chrome
File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Firefox
File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:
OperaDriver: https://github.com/operasoftware/operadriver/pull/93
SafariDriver: https://github.com/SeleniumHQ/selenium/pull/87
Opera
This API is similar to the FirefoxDriver.
File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);
// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Safari
This API is similar to the ChromeDriver.
File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);
// Option 1: Locally.
WebDriver driver = new SafariDriver(options);
// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Internet Explorer
Good luck.
Short answer: no
Installing a browser extension is outside of the scope of handling in Selenium.
In Chrome, it displays a modal window that is not "clickable" with Selenium when you want to add a plugin or app. Chrome does not require restarting.
Firefox has the same kind of behaviour to prompt for extension permissions.
You can try something that resides outside of the browser to do what you want. Sikuli might do the trick.

WebDriver not opening URL

I'm very new to selenium so I'm having trouble spotting the problem with my code. I'm using a webDriver backed selenium object, it starts the driver but never opens the URL and the driver just closes after a few moments. The last time this happened to me it was just because I had left "http" out of the URL. So what's causing it this time?
public void testImages() throws Exception {
Selenium selenium = new WebDriverBackedSelenium(driver, "http://www.testsite.com/login");
System.out.println(selenium.getXpathCount("//img"));
}
The setup looks like:
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
Thread.sleep(2000);
}
The teardown method just consists of driver.close().
I'm using selenium 2.14 and the testNG Eclipse plug-in.
You might need to do the following
selenium.open("www.testsite.com/login");
Check out this example from the selenium site:
// You may use any WebDriver implementation. Firefox is used here as an example
WebDriver driver = new FirefoxDriver();
// A "base url", used by selenium to resolve relative URLs
String baseUrl = "http://www.google.com";
// Create the Selenium implementation
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
// Perform actions with selenium
selenium.open("http://www.google.com");
selenium.type("name=q", "cheese");
selenium.click("name=btnG");
// Get the underlying WebDriver implementation back. This will refer to the
// same WebDriver instance as the "driver" variable above.
WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getUnderlyingWebDriver();
//Finally, close the browser. Call stop on the WebDriverBackedSelenium instance
//instead of calling driver.quit(). Otherwise, the JVM will continue running after
//the browser has been closed.
selenium.stop();
link to selenium
You would need to add driver.get(url) like below.
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User1\\Desktop\\chromedriver_win_16.0.902.0\\chromedriver.exe");
driver = new ChromeDriver();
driver.get("http://www.testsite.com/login");
}