How to enable flash in Selenium with headless chrome - java

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..

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 :)

Headless Chrome - getting blank page source

I'm trying to load a website with Chrome browser in headless mode using Selenium web driver. I face an issue with some specific websites. The page is loading, in the first 2-3 seconds it shows a page with "please enable javascript..." and after 3 seconds, page source goes blank.
I'm using Selenium and especially Chrome for long time and I am familiar with the platform. For the purpose of this case, I'm using Chrome Version 73.0.3683.86 , ChromeDriver 2.46.628411 (which is compatible according to Which ChromeDriver version is compatible with which Chrome Browser version?) on a Mac OS. selenium java version is latest - 3.141.59
I suspect that headless Chrome cannot handle specific content-type such as "svg" and any other GUI related HTTP response.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://identity.tescobank.com/login");
Thread.sleep(3000);
System.out.println(driver.getPageSource());
driver.quit();
Expected result is to have the page source same as it is showing in non-headless mode.
Headless Chrome should be able to handle everything the normal Chrome can do:
It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
(see https://developers.google.com/web/updates/2017/04/headless-chrome)
Since only the login page of a bank causes you trouble, my guess is that the security of the page detects an anomaly and decides not to serve you.
One way they can do that is by looking at the User Agent string which contains HeadlessChrome.
That said, unless you're writing integration tests for the bank, your behavior is at least suspicious. If you have a valid and legal concern, clear it with the bank first. They might take actions against you, otherwise. Blocking your IP address (which could affect many people) or asking the police to have a word with you.
I was facing similar issue in my script, after login. Somehow refreshing the page resolved the issue.
driver.navigate().refresh();

chrome v62 enable flash

I have a selenium (v2.53) test that visits a site containing flash player (I'm testing this player). up until now, everything was working fine, but after I updated chrome to v62, flash is disabled by default.
I can't change manually flash setting since this test is automated and running on remote machines.
I've tried adding some chrome capabilities that should work on previous versions of chrome, but it did not work on chrome 62 version since allowing flash is not enough, now a list of allowed site is also required.
How can I change both the enabled status and the list of sites using selenium?
Also, is there a way to install chrome with a config file that both enables flash and populates the required sites list?
Thanks.
P.S. I'm working with Java 8
Your best bet is to simply use Chrome options. Why do you need a config file? That sounds overly complicated and unnecessary. You can enable it through chrome preferences. Try a fresh install of Chrome too.
Something akin to the likes of:
chromeOptions:{
args: ["--allow-running-insecure-content", "--allow-insecure-websocket-from-https-origin", "allow-outdated-plugins"]
You didn't specify which language so I can't give you a language example.

Selenium Chrome Driver Limitations Web Scraping at Scale

I'm planning to use Selenium Chrome Driver for my project which will be used to do web scraping to multiple public websites (something like kayak or skyscanner). So there will be a REST GET endpoint where my backend would launch headless Chrome to scrape multiple websites, and eventually return a manipulated JSON.
I want to know how scalable is Chrome Driver as it sounds like a headless Chrome instance needs to be launched whenever a request comes in.
Updated: Question using Google Chrome Headless
Please find the pros and cons of phantom js which I noticed during implementation .Hope this helps.
Cons:
1)It will fail to recognize the browser elements like id,xpath,csselector
when compared to chrome driver.
2)If you have login mechanism ,redirects won't work as you expect when compared to chrome driver.
3)You need to manually implement the custom logic for screen shots for the test failures if you need it.
4)If you want to switch between multiple drivers like chrome,html etc then it is very difficult
Pros:
1)Test case execution is faster when compared to chrome driver
2)No browser is required it will run without GUI.
3)No much configurations are needed when compared to chromedriver.
You can go with html driver also which is quite faster then phantom but even it has its own limitations that you need take care of before implementation.
I am not sure that you really need to use PhantomJS.
Chrome implemented "headless" mode couple of months ago.
"Headless Chrome" does the same job that PhantomJS, and does it better.
I heard that PhantomJS authors even said that they will not support it anymore.
You can enable headless mode in Selenide with just on line:
Configuration.headless = true;
Did you think about headless chrome?
Headless Chrome

Categories

Resources