How to set set capability for chrome in selenium? - java

How to use this method? What parameter should I pass to "capabilityName" and "value"?
public void setCapability(java.lang.String capabilityName,java.lang.String value);

For Chrome specifically, you can find the capabilities here: https://sites.google.com/a/chromium.org/chromedriver/capabilities-aka-chromeoptions
There is a more general overview of capabilities on the selenium wiki: http://code.google.com/p/selenium/wiki/DesiredCapabilities

Here is an example that maximizes the browser window (for chrome). Enable/Disable the second while running it and see the difference.
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");

Related

Selenium Launches Firefox While Silently Ignoring Profile Preference

There are currently two recommended ways to launch the latest version of Selenium using a specific profile. One is through the Desired Capabilities object and the other us by using Firefox Options.
Desired Capabilities:
public static WebDriver launchFirefoxProfileByFFCapabilities()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(FirefoxDriver.PROFILE, firefoxProfileByProfilesIni);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
driver = new FirefoxDriver(opt);
return driver;
}
Firefox Options:
public static WebDriver launchFirefoxProfileByFFOptions()
{
WebDriver driver = null;
System.setProperty("webdriver.gecko.driver", "PATH");
ProfilesIni profile = new ProfilesIni();
String profileName = "Profile_1";
FirefoxProfile firefoxProfileByProfilesIni = profile.getProfile(profileName);
FirefoxOptions opt = new FirefoxOptions();
opt.setProfile(firefoxProfileByProfilesIni);
driver = new FirefoxDriver(opt);
return driver;
}
I noticed a few interesting discrepancies between the two approaches and I'm trying to understand what is the reason for these discrepancies and how to prevent them.
1) With Firefox Options when the specified Profile Name does not exist (as it appears in the Firefox - Choose User Profile dialog) throws WebDriverException: Unexpected value for profile: null. However with Desired Capabilities no such exception is thrown and Selenium silently ignores the profile preference (which is not ideal)
2) If the Profile Name exists (as it appears in the Firefox - Choose User Profile dialog) but the path in profiles.ini file is incorrect even Firefox Options will launch silently ignore the profile preference and launch WebDriver not connected to any profile
What is the cause of these discrepancies? How can they be prevented?
Thanks
It seems that Capabilities and FirefoxProfile/FirefoxOptions handle the profileIni in a different way.
Could you please modify and add the stack-traces so we can further help you?

How to pass a headless option for my driver using Java and Selenium?

I am setting up a chrome driver with the help of Selenium and Java. I want this driver to be executed headless but I can't find out the way. Can you explain to me what do I need to do?
My code sample:
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH);
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(DEFAULT_IMPLICITY_TIME, TimeUnit.SECONDS);
System.setProperty(CHROME_PROPERTY, LINUX_CHROMEDRIVER_PATH); // OS and Browser options
ChromeOptions options = new ChromeOptions(); // create options instance
options.addArguments("--headless"); // add an option
driver = new ChromeDriver(options); // create a driver with the specific options instance
You just need to create a ChromeOptions object in which you need to save the options for your own driver.
To add your own options just use this: options.addArguments(); and in the parenthesis insert your option in string mode.
For more details and documentation please also check here:
http://chromedriver.chromium.org/capabilities
I think this is going to work.

Is this correct - FirefoxDriver driver = new FirefoxDriver();?

I have seen almost everybody using the statement WebDriver driver=new FirefoxDriver(); here we have created an instance of FirefoxDriver class having type as Webdriver;
What if i directly create an instance of FirefoxDriver as FirefoxDriver driver = new FirefoxDriver();
I have gone through many discussion where it is said that Webdriver is an interface. I know what interface is and how it works. I want to know if FirefoxDriver driver = new FirefoxDriver(); is correct when i know that Firefox is the only browser i am gonna use in my selenium script?
As you mentioned in your question that know that Firefox is the only browser you are gonna use in your selenium script then there is no constraint in using the following line of code :
FirefoxDriver driver = new FirefoxDriver();
Here you can find a detailed discussion on What is the difference between ChromeDriver and WebDriver in selenium?

Clear cache before running some Selenium WebDriver tests using Java

I am working on Selenium WebDriver automation in java programming language. In my test suite that initiates the browser window once and perform all the tests. I want to clear the browser cache before running some tests without restarting the browser. Is there any command/function, that can achieve the purpose? Thanks.
This is what I use in Python:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
You can try converting these into Java. Hope this will help! :)
At least in Chrome, I strongly believe that if you go incognito you wont to have to clean up your cookies. You can set your options like following (the :
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def _options():
options = Options()
options.add_argument('--ignore-certificate-errors')
#options.add_argument("--test-type")
options.add_argument("--headless")
options.add_argument("--incognito")
options.add_argument('--disable-gpu') if os.name == 'nt' else None # Windows workaround
options.add_argument("--verbose")
return options
and call like this:
with webdriver.Chrome(options=options) as driver:
driver.implicitly_wait(conf["implicitly_wait"])
driver.get(conf["url"])
For IE
DesiredCapabilities ieCap = DesiredCapabilities.internetExplorer();
ieCap.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
For Chrome:
https://code.google.com/p/chromedriver/issues/detail?id=583
To delete cookies:
driver.manage().deleteAllCookies();
The following code is based on #An Khang 's answers. and it is working properly on Chrome 78.
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.manage().deleteAllCookies();
chromeDriver.get("chrome://settings/clearBrowserData");
chromeDriver.findElementByXPath("//settings-ui").sendKeys(Keys.ENTER);
return chromeDriver;
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);
On Google chrome you can use this script:
driver.get("chrome://settings/clearBrowserData");
JavascriptExecutor jse = (JavascriptExecutor)driver;
WebElement clearData = (WebElement) jse.executeScript("return document.querySelector(\"body > settings-ui\").shadowRoot.querySelector(\"#main\").shadowRoot.querySelector(\"settings-basic-page\").shadowRoot.querySelector(\"#basicPage > settings-section:nth-child(8) > settings-privacy-page\").shadowRoot.querySelector(\"settings-clear-browsing-data-dialog\").shadowRoot.querySelector(\"#clearBrowsingDataConfirm\")");
((JavascriptExecutor)driver).executeScript("arguments[0].click();", clearData);
import org.openqa.selenium.Keys;
you need to import the Keys in newer version and change the last line to findElement by xpath
WebDriver driver = new ChromeDriver();
driver.manage().deleteAllCookies();
driver.get("chrome://settings/clearBrowserData");
driver.findElement(By.xpath("//settings-ui")).sendKeys(Keys.ENTER);

How to change language in web page using Selenium(Java)?

Is their a way to pre-define a language for webpage? I want to load google page in Spanish, can we set property of webdriver in such a way that it load the page in Spanish.
Check this link:
For Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference( "intl.accept_languages", "es" );
WebDriver driver = new FirefoxDriver(profile);
For Chrome:
ChromeOptions options = new ChromeOptions();
options.AddArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);

Categories