Problems headless chromedriver downloads - java

I have an automation process created with java selenium-webdriver, chromedriver and work with eclipse. The point is that by doing the process with headless and disable gpu I get that the download is correct but it does not appear in the directory and instead if the headless option and gpu if it appears in the directory. Attached the code below and if there is someone who knows how to do it and can help me, I will be grateful, thanks.
//*********PARA CHROME*******
System.setProperty("webdriver.chrome.driver","C:\Users\Dani\Desktop\driver\chromedriver235\chromedriver.exe");//localiz
version 2.35 chromedriver
String downloadFilepath = "C:\\Users\\Dani\\Desktop\\FACTURAS";
HashMap<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.default_content_settings.popups", 0);//ventanas emergentes
prefs.put("download.prompt_for_download", false);
prefs.put("download.default_directory", downloadFilepath);//directorio para descargas local
prefs.put("download.prompt_for_download", false);
prefs.put("download.directory_upgrade", true);
prefs.put("profile.default_content_setting_values.automatic_downloads", 1);
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
options.addArguments("--headless");
options.addArguments("--window-size=1920,1200");//necesario para el headless (tamaƱo pantalla)
options.addArguments("--disable-gpu");
options.addArguments("--no-sandbox");
options.addArguments("--disable-popup-blocking");
options.addArguments("--disable-notifications");//desactivar notificaciones
options.addArguments("--disable-infobars");//evitar que apareguin infobars
options.setExperimentalOption("prefs", prefs);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);// Bydefault it will accepts all popups.
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);//indicamos segundos entre acciones

Related

The page I'm trying to access doesn't load when I use headless mode, but when I don't use it, it loads and performs the tests perfectly

I'm doing a test with selenium and java in eclipse, with chrome 62. And when I run it with headless mode enabled, the page I'm trying to access doesn't load. And if I don't activate it, yes it does.
Map<String, Object> prefs = new HashMap<String, Object>();
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(BINARY_PATH);
WebDriverManager.chromedriver().browserVersion("62").driverVersion("2.35").cachePath(EXECUTION_PATH).setup();
chromeOptions.setAcceptInsecureCerts(true);
chromeOptions.setHeadless(true);
prefs.put("profile.default_content_settings.popups", 0);
DriverFactory driverFactory = DriverFactory.getInstance();
driverFactory.setDownloadPath(
EXECUTION_PATH + Thread.currentThread().getName() + System.getProperty("file.separator"));
prefs.put("download.default_directory", driverFactory.getDownloadPath());
prefs.put("download.prompt_for_download", false);
chromeOptions.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(chromeOptions);

Selenium can't find WebElements when trying to use headless chromedriver option

I am running selenium with chromedriver and it all works fine.
Lately I tried to work with 10 chromedrivers simultaneously and it takes all the memory available, so I tried to solve it with headless option in ChromeOptions.
Tried those options:
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
But when the program runs with --headless option it won't work. (it worked before without the headless)
this is how I setup my chromeDriver:
private ChromeDriver setupChromeDriver(String proxyAddress, String downloadFilePath){
try{
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilePath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("--window-size=1920,1200");
options.addArguments("--ignore-certificate-errors");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
if(!proxyAddress.equals(""))
cap.setCapability("proxy", setupChromeProxy(proxyAddress));
ChromeDriverService chromeDriverService = new ChromeDriverService.Builder().build();
options.merge(cap);
logger.info("Selenium chrome driver set up with updated download location");
return new ChromeDriver(chromeDriverService, options);
}
catch (Exception e){
logger.info(e.getMessage());
logger.info(BaseUtilities.getStackTrace(e));
e.printStackTrace();
return null;
}
}
example how I find element which works without the headless option:
driver.findElement(By.xpath("//input[#id='identifierId']")).sendKeys(dumper.getUsername() + Keys.ENTER);
I am using Windows 10, chrome Version 75.0.3770.142 (Official Build) (64-bit),
ChromeDriver 75.0.3770.140
Tried to find some information in similar posts but couldn't find a working solution.
Thank you.
You can simply try like this
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver wDriver = new ChromeDriver(chromeOptions);

can i enable/disable image loading setting in selenium with the same driver object

final DesiredCapabilities capabilities = DesiredCapabilities.chrome();
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
Map<String, Object> prefs = new LinkedHashMap<>();
prefs.put("profile.default_content_setting_values.images", 2);
chromeOptions.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.example.com/");
This code will stop the image loading. Now I want to re-enable the Image loading again with the same driver object ??
prefs.put("profile.default_content_setting_values.images", 0);
chromeOptions.setExperimentalOption("prefs", prefs);
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
driver = new ChromeDriver(capabilities);
driver.get("https://connect.secure.wellsfargo.com/auth/login/present?origin=yodlee");
I am using this above code to changing the pref again to start loading the images again but with this code, new chrome is opening
driver = new ChromeDriver(capabilities)
but I want this with the same chrome driver.
My simple ask is to enable/disable image loading with the same driver object ?
it will be great if some body help me on this

ChromeDriver(Capabilities capabilities) is deprecated

I use ChromeDriver 2.33 with WebDriver 3.6.0 and try to set default directory for file download.
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", Vars.DOWNLOAD_FOLDER_ROOT);
DesiredCapabilities caps = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
options.setExperimentalOption("prefs", prefs);
caps.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(caps);
I found this in docs:
Use ChromeDriver(ChromeOptions) instead. Creates a new ChromeDriver
instance. The capabilities will be passed to the chromedriver service.
I hope you wanted to ask about the workaround to avoid the deprecation.
The old method of just building with Capabilities is deprecated. Now, it takes a ChromeDriverService & Capabilities as parameters. So, just a build a ChromeDriverService and pass the same along with your Capabilities to remove the deprecation warning.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/usr/local/chromedriver"))
.usingAnyFreePort()
.build();
ChromeDriver driver = new ChromeDriver(service, capabilities);
EDIT:
Since ChromeDriver(service, capabilities) is deprecated now as well, you can use,
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/usr/local/chromedriver"))
.usingAnyFreePort()
.build();
ChromeOptions options = new ChromeOptions();
options.merge(capabilities);
ChromeDriver driver = new ChromeDriver(service, options);
However, You can completely skip DesiredCapabilities and use only ChromeOptions with setCapability method like,
ChromeOptions options = new ChromeOptions();
options.setCapability("capability_name", "capability_value");
driver = new ChromeDriver(options);
The new way to use chrome capabilities is like this :
ChromeOptions options = new ChromeOptions();
// Proxy proxy = new Proxy();
// proxy.setHttpProxy("myhttpproxy:3337");
// options.setCapability("proxy", proxy);
// options.addArguments("--headless");
// options.addArguments("--disable-gpu");
// options.setAcceptInsecureCerts(true);
// options.addArguments("--allow-insecure-localhost");
// options.addArguments("--lang=fr-CA");
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
You can get more options by looking at this site : https://sites.google.com/a/chromium.org/chromedriver/capabilities

How to download a file from a website in a specific folder, I am using Chorme

I'm trying to download a file from a website that has to be saved in a specific folder. Website http://bookboon.com/en/basics-of-accounting-information-processing-ebook
When I click on download it saves the file in the download section, I also tried to change the download directory in chrome settings, it doesn't work. I am trying automation (selenium, java). Is there any way?
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User_2\\Downloads\\chromedriver_win32\\chromedriver.exe");
d = new ChromeDriver();
d.get("http://bookboon.com/en/basics-of-accounting-information-processing-ebook");
d.findElement(By.id("email")).sendKeys("asd#ymail.com");
WebElement One=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[1]/input"));
One.sendKeys("Studying");
One.sendKeys(Keys.TAB);
WebElement Two=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[2]/input"));
Two.sendKeys("Engineer/Science MSc");
Two.sendKeys(Keys.TAB);
WebElement Three=d.findElement(By.xpath("html/body/div[1]/div/article/div/section[1]/form/div[2]/div[2]/div[3]/input"));
Three.sendKeys("All India Institute of Medical Sciences (AIIMS), Delhi");
Three.sendKeys(Keys.TAB);
d.navigate().back();
downlinks = d.findElements(By.className("pdf"));
}
}
For Chromedriver It will work
String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
For Firefox: You need to setPreference
profile.setPreference("browser.download.dir", "Filepath");
Hope this helps. :)

Categories