Disable developer mode extensions error in Selenium Java - java

I am working with Java Selenium. I came across the following error:
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.gezinomi.com/");
Picture of error:

This is not an error, since if you click Cancel option, it'll just follow the path normally.
However, it is probably causing the test to fail, once the browser remains waiting the confirmation. You can disable it through Selenium Java code, as already shown here:
ChromeOptions options = new ChromeOptions();
options.addArguments("chrome.switches","--disable-extensions");
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.gezinomi.com/");

That popup was problematic while running tests remotely on SauceLabs. I tried the ChromeOptions argument "--disable-extensions" which didn't work. I gave up, accepting that a chunk of screen would not be visible.
As soon as I stopped researching, I came across this reference which clearly defines the Chrome option as "--disable-extensions-file-access-check".
I don't know why this was so hard to find, but hopefully it can help others waste LESS of their time.

Related

Running Selenium in headless mode via Linux causes errors

I have a large set of tests running on a site.
When I run the test locally on Windows - they all pass on 100%. The test are designed and running on Google Chrome.
Now, we have started to run the tests on Linux via Jenkins jobs on headless mode. Some tests now fail on 0% or only pass on 20% or even 10%. In my code I'm finding elements by ID, xpath or css and simple click on them. And I use the WebDriverWait object for waiting - both for the element to be present and to be clickable.
Example of my code:
WebDriverWait wait = new WebDriverWait(browser, secondsToWait);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id(elementID)));
lastFoundElement = wait.until(ExpectedConditions.elementToBeClickable(By.id(elementID)));
clickLastFoundElement();
In my report I see mostly that the elements were not found and that I passed the timeout set in the wait object.
How to make headless tests be more stable?
Why the headless state causes so many problems?
It is actually known issue to Selenium community that headless is not stable as it should be, read here about the issue more
Chrome runs very unstable when headless mode is activated.
There are multiple different issues and bugs depending on: Chrome Version, ChromeDriver Version and the executed tests.
Issues and Fixes (occured so far):
Chrome does not start in headless mode
Exception:
No informative Error message. Only a timeout exception when navigate() is called on the driver:
org.openqa.selenium.TimeoutException: timeout
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome is very slow in headless mode
Fix:
options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
Chrome does not operate correctly after a certain time, when tests run very long resp. many actions are executed in one test session
Exception:
... Timed out receiving message from renderer: ...
Fix (not tested yet!):
options.addArguments("--disable-browser-side-navigation");
You possibly missing setting the headless window size.
Try this settings:
Map<String,String> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadsPath); // Bypass default download directory in Chrome
prefs.put("safebrowsing.enabled", "false"); // Bypass warning message, keep file anyway (for .exe, .jar, etc.)
ChromeOptions opts = new ChromeOptions();
opts.setExperimentalOption("prefs", prefs);
opts.addArguments("--headless", "--disable-gpu", "--window-size=1920,1080","--ignore-certificate-errors","--no-sandbox", "--disable-dev-shm-usage");
driver = new ChromeDriver(opts);
Here I put much more setting used by me for the headless chrome.
I hope this will be useful for you.
Please include screen size in the chrome option.
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("--window-position=0,0"));
options.addArguments(Arrays.asList("--window-size=1840,1080"));

ChromeDriver sitting on Data:, when launching selenium/cucumber test

I have hit a brick wall here; I am completely unable to run any selenium tests on Chrome, as every time I launch it, the chrome browser will open and hang for 600 seconds, with Data:, in the address bar. I have tried every solution under the sun found so far on stack overflow.
Verified the versions between ChromeDriver and Chrome browser - yes they match.
Uninstalled/reinstalled chrome several times, and also verified the versions match.
The interesting thing is that running the tests on Microsoft Edge work fine, which leads me to believe there could be an issue with Chrome/Chromedriver not having the correct permissions? The machine the tests are running on is managed by my company. Any and all help would be greatly appreciated here.
Thanks :)
I think your issue is in the configuration of the WebDriver object.
If you use Maven, there is a library which helps configuring the webdrivers. Or if you aren't, then you can download the .jar file and add it to your build path.
https://github.com/bonigarcia/webdrivermanager
With this, you don't need to download the driver file itself and set the system property for it's value.
Here is a quick example for creating the ChromeDriver object:
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
The same thing on Edge:
WebDriverManager.edgedriver().setup();
WebDriver driver = new EdgeDriver();
Try this at your WebDriver configuration and maybe it can help!
Happy coding! :)
I have solved this problem - the administrators of the computers I was using had not given access to Chrome's developer tools. After getting access, the problem had gone away. Hope this helps anyone else who encounters this problem :)

Selenium with Firefox not clicking on elements located using XPath

I have a Selenium Java based automation infrastructure. Most elements locators are using XPath since our web site uses several templates where elements can be located based on their texts only.
It is working just perfect on Chrome however it doesn't work on Firefox.
The most basic action - element.click() doesn't work with Selenium on Firefox while it works perfect on Chrome!
There is no exceptions thrown, just nothing done.
I do not use any kind of very complex xPath expression, mostly something like //*[contains(text(),'Some unique text')] that gives a clear unique element locator and each click() is performed after getting the element visible with methods like this:
public void clickVisible(String xpath){
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
driver.findElement(By.xpath(xpath)).click();
}
While running and debugging the tests I saw that elements are found visible shortly while running on Firefox too, however click() simply doesn't work there.
In case I locate those elements with css selector it works on Firefox perfect!
I already heard that xPath engines are different in each browser and I saw several old posts here dealing with xPath related problems, but mostly with IE, not with Firefox, and most of that posts are written many years ago.
Also, I worked with Firefox several years ago and there were no such problems that time!
I'm trying to understand what happened with Selenium on Firefox so it is not performing very basic commands for xPath located elements?
I couldn't find any clear documentation / explanation about this.
This post looks interesting, however I couldn't try setting capability.setCapability("marionette", false); since my Firefox options are set in the following manner:
FirefoxProfile profile = new FirefoxProfile();
FirefoxOptions options = new FirefoxOptions();
profile.setPreference("browser.download.dir",downloadsPath);
--------
options.setProfile(profile);
driver = new FirefoxDriver(options);
Are you running the test in headless mode. If so you have to maximize the screen by setting it in environment variables.
os.environ['MOZ_HEADLESS_WIDTH'] = '1920'
os.environ['MOZ_HEADLESS_HEIGHT'] = '1080'
If not headless please update the firefox options to the below size to fix the issue.
firefoxOptions.AddArgument("--width=1920");
firefoxOptions.AddArgument("--height=1080");
Arjun

How to enable flash in Selenium with headless chrome

I'm trying to automate some interactions with our flash app as part of our CI process. I'm running into troubles with enabling flash when running chrome headlessly (via xvfb-run) with Selenium Standalone Server. I've done a lot of searching, but thus far haven't come up with anything that works.
I'm currently using this, but am open to switching to different versions if there's a known working config somewhere...
Selenium Standalone Server 3.11
Chromedriver 2.33
Chrome 65.0.3325.181
Java 8
When I first got this started I would get a warning on the page saying I needed to enable Adobe Flash Player. I got "past" that message by using the following from https://sqa.stackexchange.com/questions/30312/enable-flash-player-on-chrome-62-while-running-selenium-test:
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_setting_values.plugins", 1);
prefs.put("profile.content_settings.plugin_whitelist.adobe-flash-player", 1);
prefs.put("profile.content_settings.exceptions.plugins.*,*.per_resource.adobe-flash-player", 1);
// Enable Flash for this site
prefs.put("PluginsAllowedForUrls", "ourapp.com");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
driver.get("ourapp.com");
When loading our app, the page now gives a slightly different message which I haven't been able to get past. Is there a way to get around this, or is there any other way to enable Flash by default?
Restart Chrome to enable Adobe Flash Player
Thanks in advance for the help!
Thanks to a coworker for pointing out this post indicating that plugins don't work in headless chrome. https://groups.google.com/a/chromium.org/forum/#!searchin/headless-dev/flash%7Csort:date/headless-dev/mC0REfsA7vo/rKAZdRrCCQAJ
Fortunately in my case I was already using xvfb as a virtual display, so removing the "headless" argument from my ChromeOptions was all I needed to get everything running.
EDIT*
While true that I did not need to run in headless mode while using xvfb, I ended up finding that the real 'solution' (more of a workaround) to my problem was to upload a custom chrome profile into my docker image. Doing so allowed me to set all the preferences I needed, and none of the code posted in the original post is required. Would much prefer to achieve this programatically, but this at least gets me what I need for now. Figured I'd post in case someone else runs into this in the future..

Selenium Web Driver - How to run Firefox in the Background

Every time I run my tests on Firefox browser the browser takes active control and kind of disturbs/interrupts my work. Is there way/command to make it run on the background.
You can set position of the browser outside of your screen:
FirefoxDriver firefoxDriver = new FirefoxDriver();
firefoxDriver.manage().window().setPosition(new Point(-x, -y));
See Also:
How to hide Firefox window (Selenium WebDriver)?
How to run ghostdriver with Selenium using java
As there isn't any accepted answer - you can download firefox addon and install it with your instance of firefox. Addon is named FireTray it can launch firefox minimized in tray so there won't be any poping up windows etc. It can hide 1 window but with litle tinkering with addon you can change that value to anything. Here is some code to help you understand what I'm talkim about :)
FirefoxProfile prof = new FirefoxProfile(new File(FIRE_TRAY_PATH));
WebDriver driver = new FirefoxDriver(prof);
Now you have pseudo-background firefox browser :)
You could try using an HtmlUnitDriver with Firefox capabilities for a 'headless' effect.
new HtmlUnitDriver(DesiredCapabilities.firefox())
HtmlUnitDriver doesn't play nice with javascript interactions all the
time
Alternatively, add a resize to your existing driver on startup to hide the window.
driver.manage().window().setSize(new Dimension(0, 0));
This might have an impact on the driver's ability to resolve
WebElements that are not on the page and change your behavior. I'm
not sure

Categories

Resources