How can I get a screenshot in headless ChromeDriver? - java

I am trying to capture screenshots in a headless ChromeDriver instance. I can get a proper screenshot without running headless.
I have tried to just follow an example for this, however, the example is not working properly.
public static void main(String[] args) throws Exception{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome");
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.navigate().to("https://adobe.com");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(srcFile, new File("testscreenshot.png"));
driver.quit();
}
The screenshot the code above generates will be blank white, with a scroll bar on the side. What can I do to capture a screenshot in headless mode? Is this possible?

This behavior was different on Windows, Ubuntu, and Mac. I am currently working on Mac. It was not rendering the page for some reason. This was all I needed before I took my screenshot
new WebDriverWait(driver, 10)

Related

ChromeDriver doesn't see Chrome Print Dialog Box only in headless mode

I'm having issues with running chrome driver in headless mode and interacting with Print Dialog box. It seems that headless chrome doesn't even open Print Dialog Box.
Here is my code for clicking on Save As Pdf button
private static void clickOnSaveAsPdf() {
WebElement saveAsPdfButton = chromeDriver.findElement(By.xpath("//*[#id=\"root\"]/div/div[3]/div[1]/main/div/div/div[1]/div[3]/div/button[2]"));
saveAsPdfButton.click();
}
This part of code above is executed without issues every time. So the issues start when i try to switch windows and focus on print dialog box. When I execute this line of code:
System.out.println(chromeDriver.getWindowHandles());
I get different results when in headless and when in non-headless.
In non-headless mode result I'm getting is: [CDwindow-55CA15B96D8DB58A454CC30B30F0F970, CDwindow-8CB30AB28BC2E8BCDE395BAA9F7B9101]
But in headless mode, result I'm getting is: [CDwindow-15ED0AA78CE73CA53CA71E6FB2A94998]
So, obviously, headless chrome doesn't see print dialog box. Is there any way I can access it? Here is the method that starts ChromeDriver, with its options and prefs:
public static void runChromeDriver() {
String chromeDriverPath = "/home/nemanja/Downloads/chromedriver_linux64/chromedriver";
System.setProperty("webdriver.chrome.driver", chromeDriverPath);
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--window-size=1920, 1080");
options.addArguments("--ignore-certificate-errors");
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);
prefs.put("download.default_directory", getFullPathToTempDirectory());
prefs.put("download.prompt_for_download", false);
prefs.put("download.directory_upgrade", true);
prefs.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", prefs);
chromeDriver = new ChromeDriver(options);
}
Some further info, I'm using Java 17, ChromeDriver v111 and my OS is Ubuntu 22.04.
Before you invoke getWindowHandles() ensure that the second window handle is present inducing WebDriverWait for numberOfWindowsToBe(n) as follows:
new WebDriverWait(driver, Duration.ofSeconds(5)).until(ExpectedConditions.numberOfWindowsToBe(2));
System.out.println(chromeDriver.getWindowHandles());

Remove address bar in IE with selenium

I can't hide the address bar using selenium
I have read about selenium's capabilities with IE but I can't find the specific one
I hope to have an IE window without the address bar using selenium integrated with java
This is my code:
public class SeleniumIE {
static WebDriver visor = null;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "C:\\Users\\...\\Documents\\NetBeansProjects\\SeleniumIE\\IEDriverServer_Win32_3.14.0\\IEDriverServer.exe");
InternetExplorerOptions options = new InternetExplorerOptions();
//commented lines do not work
//options.setCapability("NoNavBar", false);
//options.setCapability("toolbars", false);
visor = new InternetExplorerDriver(options);
visor.get("https://www.google.com/");
visor.quit();
}
}
You can use the Robot class to maximize the window by activating the F11 key. IE does not show any toolbars when it is maximized.
Robot r = new Robot();
r.keyPress(KeyEvent.VK_F11);
r.keyRelease(KeyEvent.VK_F11)
Alternatively, Selenium does have capabilities that allow for browser maximization:
driver = new FirefoxDriver();
driver.manage().window().maximize();
You could send key stroke with F11 to make IE11 run in full screen.
Or I think kiosk mode can meet your requirement.
When you run Internet Explorer in Kiosk mode, the Internet Explorer title bar, menus, toolbars, and status bar are not displayed and Internet Explorer runs in Full Screen mode.
When you run iexplore -k page, IE will start in kiosk mode. You could check this thread and this blog for more information.

opening chrome with selenium - weird behavior when opening it with my user

this code does not redirect me to Instagram. when i debug it i see the program just stuck on the "new chrome driver" row. but when i delete the row in the middle (add arguments) it doesnt stuck and does open instagram! didnt find any explanation for this. can someone help?
System.setProperty("webdriver.chrome.driver", "C:\\Users\\gum\\Desktop\\intellijprojects\\Instagram\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/gum/AppData/Local/Google/Chrome/User Data");
ChromeDriver driver = new ChromeDriver(options);
driver.get("https://www.instagram.com/explore/tags/food/");

Selenium- After click a link,IE opens two windows instead of one window

I'm working on the secured web application. When I click link within frame, it opened another single window where information to be filled.But when I execute this scenario in selenium, it click the link within frame and system display two windows where window1 shows Blank page with title as "Blank Page- window internet explorer' and window2 shows website security certificate with no title.
When I'm doing manually, it showing single window but during automation, it shows two windows.
Note: Application support only IE10.
script:
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe");
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get(url);
driver.navigate().to("javascript:document.getElementById('overridelink').click()");
Login the application and next step to click link
driver.findElement(By.xpath(".//table[#id='maintable']//a").click();
Please help me on this.
I encountered the exact same issue in IE 10..The issue appears to be resolved when I am setting "nativeEvents" to true using the DesiredCapabilities Class. You may try the same and let us know if it also works for you. Please find code segments for your reference:
DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer();
ieCapabilities.setCapability("nativeEvents", true);
WebDriver driver = new InternetExplorerDriver(ieCapabilities);
The 2nd Line seems to do the trick.
The below solution worked
Modifying the registry value , TabProcGrowth to 0 solved the issue-
Go to HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main
TabProcGrowth (right-click) → Modify… → Value data: 0
which version of selenium jar are you using. Try below code...
System.setProperty("webdriver.ie.driver","./tools/IEDriverServer_32.exe"); WebDriver driver = new InternetExplorerDriver();
driver.get(url); driver.navigate().to("javascript:document.getElementById('overridelink').click()‌​");
if this not works.... last option, please Reinstall IE and problme will be fixed.

Selenium Webdriver IE could not find element

I am trying to navigate to www.google.com and send some inputs to search box using Selenium webdriver with Internet Explorer(IE).
static WebDriver webDriver = null;
static DesiredCapabilities IEDesiredCapabilities = DesiredCapabilities.internetExplorer();
System.setProperty("webdriver.chrome.driver", TestConstants.chromeDriverFilePath);
System.setProperty("webdriver.ie.driver", TestConstants.IEDriverFilePth);
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
webDriver = new InternetExplorerDriver(IEDesiredCapabilities);
//webDriver = new ChromeDriver();
webDriver.navigate().to("http://www.google.com");
webDriver.findElement(By.name("q")).sendKeys("Venkatesh Kolisetty");
//webDriver.findElement(By.id("lst-ib")).sendKeys("Venkatesh Kolisetty");
This piece of code runs very well when i use Chrome, but throws org.openqa.selenium.NoSuchElementException when IE is used.
This opens required web page in the IE browser which is opened by selenium. The problem is selenium is not able to find any element after page is loaded only when IE is used. For chrome, it finds required elements.
Is there any capability to be added in IEDesiredCapabilities
Kindly see the possibility of providing a programmatic solution instead of changing internet options manually.
Yes this is common issue when you use IE.
Open regedit.exe
Open HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Zones
So Zones will contain 0,1,2,3,4 and on right hand side three columns will be visible as soon as you click on 0 i.e. Name Type Data
Now in Name column look for 2500 Double click it. Put Value data as 3 and Base as Hexadecimal
You did this for 0.
Now repeat the same steps for 1,2,3,4..
Do this for all i.e. 0,1,2,3,4,5 => Change all 2500's value data to 3.
After that run this code.
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.ie.driver", "D:\\Selenium\\CP-SAT\\IEDriver\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("www.google.com");
It will run on IE. You need a IEDriverServer.exe as i have shown in path, which will run your IE browser.
Reply to me for further query. I ran the above code in eclipse and it ran successfully.
Happy learning :-)
Issue resolved after adding some required capabilities.
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
IEDesiredCapabilities.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.google.com");
IEDesiredCapabilities.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
IEDesiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
IEDesiredCapabilities.setJavascriptEnabled(true);
//IEDesiredCapabilities.setCapability("requireWindowFocus", true);
IEDesiredCapabilities.setCapability("enablePersistentHover", false);
IEDesiredCapabilities.setCapability("requireWindowFocus", true); is optional

Categories