Selenium FindElement and Chrome in Headless mode - selenium

After starting chromedriver.exe in headless mode following this advice and using just these arguments
options.AddArgument("headless");
options.AddArgument("window-size=1280,960");
The chromedriver opens invisibly. But Selenium's FindElement() command is not finding anything on the headless Chrome page. Instead it throws this exception:
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: no such element: Unable to locate element:
Q1: Has anyone had success running Selenium commands in Chrome's headless mode?
Q2: Have you been able to use FindElement with a chromedriver running in headless mode? If yes, how did you do it?
After reading more, perhaps something along these lines may be necessary? Add this to the Chrome startup options and then maybe connect chromedriver to it?
"remote-debugging-port=9222"
But with that option the IWebDriver and chromedriver does not open.
Background info: to answer, why would you want to do this? The primary reason was for tests run as a part of CI. These are tests that run on a VM and may not support 1080p monitors. If we ran it in headless mode and set the resolution that way we could.

Add below lines of code in your main class:
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
options.addArguments("--window-size=1920,1080");
options.addArguments("--disable-gpu");
options.addArguments("--disable-extensions"); options.setExperimentalOption("useAutomationExtension", false); options.addArguments("--proxy-server='direct://'");
options.addArguments("--proxy-bypass-list=*");
options.addArguments("--start-maximized");
options.addArguments("--headless");
WebDriver driver = new ChromeDriver(options);

Related

Blank page on headless Selenium tests run with Jenkins

Headless Selenium tests can be well run on my machine (yup, I should definitely move in this ideal place where problems doesn't exist).
However, when I launch those tests via Jenkins, none of the page elements are found. I took a screenshot to figure out why, and it shows a blank page.
This is how I instanciate my headless Chrome browser.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setPageLoadStrategy(PageLoadStrategy.NONE);
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--window-size=1920x1080");
chromeOptions.addArguments("start-maximised");
chromeOptions.addArguments("enable-automation");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--disable-browser-side-navigation");
chromeOptions.addArguments("--disable-gpu");
driver = new ChromeDriver(chromeOptions);
driver.manage().timeouts().pageLoadTimeout(30L, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(3L, TimeUnit.SECONDS);
driver.manage().window().maximize();
I have a guess but my coworker says it can't be this: should I install Xvfb on Jenkins server? I mean, is it mandatory? (I must be at least 51% sure before trying this approach ^^)
Thanks in advance.
I tried lots of solutions but it turned out that Jenkins wasn't allowed to access the resources I needed to test. So... I couldn't solve it myself anyway. Sysadmins did.

chrome driver.manage().window().maximize() exception failed to change window state to maximized [duplicate]

The release notes for ChromeDriver 2.33 says that ""Fixes a bug which caused Resizing/Positioning Window commands to fail on Chrome 62+" however this still seems to be an issue when i am using Chrome 62+ browser. Maximizing chrome window using chrome driver results in below exception. Does anyone know a solution please?
Another thing i noticed is, though i installed latest chromedriver (v2.33) from https://chromedriver.storage.googleapis.com/index.html?path=2.33/, the log printed below says Driver info: chromedriver=2.25.426923 !!
Exception in thread "main" org.openqa.selenium.WebDriverException:
unknown error: cannot get automation extension from unknown error:
page could not be found:
chrome-extension://aapnijgdinlhnhlmodcfapnahmbfebeb/_generated_background_page.html
(Session info: chrome=62.0.3202.62) (Driver info:
chromedriver=2.25.426923
(0390b88869384d6eb0d5d09729679f934aab9eed),platform=Windows NT
10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information)
There are exactly 2 issues.
As you mentioned, you have installed latest chromedriver (v2.33) but the log printed below says Driver info: chromedriver=2.25.426923, this issue must be addressed first. You can consider to manually kill all the dangling chromedriver.exe tasks from the Task Manager. Additionally you can consider to use CCleaner to wipe out all the rotten OS stuffs from your system. Take a system reboot if required. Finally ensure that what ever the absolute location of chromedriver.exe you are using within System.setProperty() ensure that the chromedriver binary is of version 2.33.
Finally, it is suggested to use ChromeOptions class to maximize the Web Browser as follows:
System.setProperty("webdriver.chrome.driver", "C:\\your_directory\\chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("disable-infobars");
opt.addArguments("--start-maximized");
opt.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(opt);
driver.get("https://google.com");
Here are some of the alternatives which may solve your question:
Using maximize() from WebDriver.Window interface :
driver.manage().window().maximize();
Using setSize(Dimension targetSize) from WebDriver.Window interface:
driver.manage().window().setSize(new Dimension(800, 600));
Using addArguments("--start-maximized") through ChromeOptions:
chromeOptions.addArguments("--start-maximized");
Using addArguments("--window-size=1920,1080") through ChromeOptions:
chromeOptions.addArguments("--window-size=1920,1080");
Using executeScript() from JavaScriptExecutor interface:
((JavaScriptExecutor)driver).executeScript("window.resizeTo(1024, 768);");
You can find a related discussion in Chrome - org.openqa.selenium.WebDriverException: unknown error: cannot get automation extension at driver.manage().window().maximize();.
I believe there were some old chrome driver processes running in backend and same were being picked up when it was invoked via code. I deleted all processes instances, deleted old version of chrome driver, added the new 2.33 version and it worked. Thanks all for your suggestions.
I think the reason behind it may be your chrome version. Try again with updating your chrome browser. I have faced this type of issues for compatibility between chrome browser & the driver
Use class ChromeOptions.
System.setProperty("webdriver.chrome.driver", "h:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get(url);

How to run chrome driver in headless mode or in background

I've been using chrome driver (with selenium webdriver), so far it never caused any issue, now for some requirement i want to run test cases in background. How can I do this?
Currently Chrome 59 supports headless mode.
You just need to use those chromeOptions to use headless chrome in selenium.
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--disable-gpu");
You can refer below link for more details:
https://www.automation99.com/2017/07/how-to-use-chrome-headless-using.html
According to this post Any way to start Google Chrome in headless mode?, a new flag --headless is available in Chromium but Selenium is still to catch up as far as my limited googling skills tell me.
In the mean time you can explore HTMLUnitDriver for headless testing.
WebDriver driver = new HtmlUnitDriver((BrowserVersion.CHROME), true);

Remove security message using Selenium WebDriver with Java

While running Selenium automated tests on Chrome, I get following type of error message:
You're using an unsupported command line flag: ignore-certificate-errors. Stability and security will suffer
Can someone please suggest how can I remove it using Selenium WebDriver with Java?
Set mode "test-type" to ChromeDriver capabilities
Here's my sample code in Java
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type", "start-maximized",
"no-default-browser-check");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
If you are using chrome version 35.x, this kind of pop-up will come. Try using chrome version 34.x and then see.
Actually its the problem with chrome builds. Just give it a try.
Thank you,
Shravan Kumar.T

How to disable Flash in selenium remote webdriver

How do I disable the loading of flash objects when using Selenium Remote WebDriver.
It will be helpful if I get a solution for the normal webdriver also.
Since in most cases the Flash object is loaded by a JavaScript
I have tried disabling the javascript on the webdriver and remote webdriver both, but it does not work.
I tried to to disable the JavaScript by:
WebDriver driver = new FirefoxDriver();
((DesiredCapabilities) driver.getCapabilities()).setJavascriptEnabled(false);
I also tried:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(false);
WebDriver driver = new FireFoxDriver(caps);
For Remote WebDriver i tried:
final DesiredCapabilities firefoxCapability = DesiredCapabilities.firefox();
firefoxCapability.setJavascriptEnabled(false);
new RemoteWebDriver(new URL("http://" + windowsIP + ":4444/wd/hub"), firefoxCapability);
After execution of the above statement the remote server displays
Executing: [new session: <platform=ANY, javascriptEnabled=false, browserName=firefox, version=>] at URL:/session>
but still all the Javascript is executing on the pages the driver loads and the Flash is also loading.
Please help me :
1. how can stop the flash from loading.
2. need it on remote driver as I need to test the pages on IE, Firefox, Chrome. Hence loading the forefox profile will not work
Thank you for the help.
I used this code on Linux mint and it works:
FirefoxProfile profile= new FirefoxProfile();
profile.setPreference("plugin.state.flash", 0);
FirefoxDriver driver = new FirefoxDriver(profile);
though it's already answered question but on different forum... so i will consolidate for you...
I'm not sure if flash objects are loaded by javascript....but if disabling javascript is problem then...
Never disable Javascript for Firefox driver, in case if you want to use it being disabled try with HTMLUNITDRIVER which specially meant for non-javascript pages.
Reason being important parts of firefox driver is implemented in javascript and disabling would have serious concerns.
HtmlUnitDriver on the other hand is fastest and best way for automation tests (splly for pages with no JS)
please check this group discussion
https://groups.google.com/forum/?fromgroups=#!topic/webdriver/daLOzCiU_h4%5B1-25%5D
I had the same problem and needed it to be solved for Chrome. This is how I got it to work:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-bundled-ppapi-flash");
WebDriver webDriver = new org.openqa.selenium.chrome.ChromeDriver(options);