Disable selenium driver detection on geckodriver using Java - java

I'd like to know if the method shared here: Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection it's still currently working and if yes I'd like to know how to do the same using Java on geckodriver.
Since now I tried the following without success:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("dom.webdriver.enabled","False");
profile.setPreference("useAutomationExtension","False");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
String user = System.getProperty("user.name");
System.setProperty("webdriver.gecko.driver", "/Users/" + user + "/Desktop/Configs/geckodriver");
FirefoxDriver driver = new FirefoxDriver(options);
driver.executeScript("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})");
driver.get("https://mysite/");
But typing navigator.webdriver in console still returns True

Related

How can i set browser language in Selenide using Java?

Hello guys please how can i do it?When i run not headless mode browser is in english and everything works fine, but when i run headless mode language is changed to my native language.
I am using this for headless mode.
Configuration.headless = true;
Selenide Configuration class contains
public static MutableCapabilities browserCapabilities which used within the driver startup if provided.
For Chrome:
ChromeOptions options = new ChromeOptions()
.setHeadless(true)
.addArguments("--lang=en_US");
Configuration.browserCapabilities = options;
But note --lang argument might be ignored on Linux.
For Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("intl.accept_languages", "en-US");
FirefoxOptions options = new FirefoxOptions()
.setHeadless(true);
.setProfile(profile);
Configuration.browserCapabilities = options;

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?

Is it required to set the DISPLAY for headless firefox browser on linux machine

I want to run the headless browser and below is the code for same.
However when i ran it. it shows "Error: no DISPLAY environment variable specified"
try {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("webdriver.log.driver", "INFO");
profile.setPreference("webdriver.log.file", targetDir + File.separator + "firefoxSeleniumServer.log");
profile.setPreference("browser.download.folderList",2);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv;text/plain");
if(platform.equalsIgnoreCase("linux")) {
DesiredCapabilities desiredCap = DesiredCapabilities.firefox();
profile.setPreference("browser.download.dir",System.getProperty("user.dir")+ File.separator + "target");
System.setProperty("webdriver.gecko.driver", "/test/geckodriver/geckodriver");
System.setProperty("webdriver.firefox.bin","/usr/bin/firefox/firefox");
desiredCap.setCapability("headless", true);
driver = new FirefoxDriver();
}
However when i set the display it shows unable to open firefox on DISPLAY:99
Also i tried setting the xvfb as well. But that also did not work.
As i am using gekco driver here, do i need to do some more configurations.
I think you are supposed to provide the DesiredCapabilities object as a parameter to the FirefoxDriver constructor:
References
The javadoc for FirefoxDriver.
What is the use of DesiredCapabilities in Selenium WebDriver?

How to Disable JavaScript in WebDriver when automating in Selenium?

I am currently trying to write a test that tests the non-JS version of my website. I use Selenium with Java. I have tried this:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("javascript.enabled", false);
driver = new FirefoxDriver(profile);
driver.get();
However this isn't working. It just loads the page with JavaScript Enabled.
As a workaround I did, this, for the requirement.
It will manually set the javascript.enabled property to false by the following script.
WebDriver driver = new FirefoxDriver();
driver.get("about:config");
Actions act = new Actions(driver);
act.sendKeys(Keys.RETURN).sendKeys("javascript.enabled").perform();
act.sendKeys(Keys.TAB).sendKeys(Keys.RETURN).perform();

How to set set capability for chrome in selenium?

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");

Categories