How to handle window pop up components using selenium [duplicate] - java

I have written a code to download an excel file, it is working, but it stops as soon as the popup appears. Automatically it should download the file and store in specified location, which is not happening now. Please anyone help in finding a solution for this problem
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);
profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "text/csv");
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir","e:\\SampleExcel");
driver.get("http://url");
driver.findElement(By.name("email")).sendKeys("abc#gmail.com");
driver.findElement(By.name("pass")).sendKeys("abc");
driver.findElement(By.id("edit-submit")).click();
driver.findElement(By.id("toolbar-link-admin-config")).click();
driver.findElement(By.linkText("Reports")).click();
driver.findElement(By.xpath("//input[#value='5']")).click();
driver.findElement(By.id("edit-submit")).click();

Try below code
FirefoxProfile profile = new FirefoxProfile();
String path = "D:\\Downloads_sel";
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", path);
profile.setPreference("browser.download.alertOnEXEOpen", false);
profile.setPreference("browser.helperApps.neverAsksaveToDisk", "application/x-msexcel,application/excel,application/x-excel,application/excel,application/x-excel,application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel");
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("browser.download.manager.focusWhenStarting", false);
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.alertOnEXEOpen", false);
profile.setPreference("browser.download.manager.closeWhenDone", false);
profile.setPreference("browser.download.manager.showAlertOnComplete", false);
profile.setPreference("browser.download.manager.useWindow", false);
profile.setPreference("browser.download.manager.showWhenStarting", false);
profile.setPreference("services.sync.prefs.sync.browser.download.manager.showWhenStarting", false);
profile.setPreference("pdfjs.disabled", true);
WebDriver driver = new FirefoxDriver(profile);
For complete MIME types list follow the link: http://qaautomationworld.blogspot.in/2014/02/file-downlaoding-using-selenium.html

Try to use this code:
FirefoxProfile profile = new FirefoxProfile();
WebDriver driver = new FirefoxDriver(profile);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk" , "application/octet-stream;application/csv;text/csv;application/vnd.ms-excel;");
profile.setPreference("browser.helperApps.alwaysAsk.force", false);
profile.setPreference("browser.download.manager.showWhenStarting",false);
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir","e:\\SampleExcel");

Please replace text by application for preference setting of not prompting download dialog box, like following:
profile.setPreference("browser.helperapps.neverAsk.saveToDisk" , "application/csv");
Just for confirmation, the file which is downloaded is with csv extension, right? If it's not like that, we'll have to make change in above code.

Related

Choose directory to download files with selenium in chrome

I need to download XLSX files using Java + Selenium in chrome, but I'm not able to choose the folder to download the files. I've tried all the other alternatives I found here, but from what I've noticed, all the others are already obsolete.
Here is my code:
String downloadFilepath = "D:\\down\\";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("prompt_for_download", false);
chromePrefs.put("directory_upgrade", true);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("--disable-notifications");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
driver = new ChromeDriver(options);
I managed to resolve it. I downloaded the last stable version that came out in July (mine was from June) of chromedriver and put the code below.
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);
WebDriver driver = new ChromeDriver(options);

How to switch to a new window and print the title in selenium webdriver (without windowhandles)

Iam new to selenium and trying to switch to new amazon tab which I opened and print the title
But when Sending control and Tab nothing happens how to achieve it can anyone help here :)
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.com");
//Thread.sleep(6000);
Actions a = new Actions(driver);
List<WebElement> el= driver.findElements(By.xpath("(//div[#class='navFooterColHead'])[1]/../ul/li/a"));
for(int i=0;i<el.size();++i)
{
el.get(i).sendKeys(Keys.Control,Keys.Enter);
}
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

Selenium Remote WebDriver get canvas content using Chrome in headless mode

I'm trying to get Whatsapp QR with Selenium WebDriver on remote machine.
I using latest version on docker package
selenium/standalone-chrome:latest
The point is, if I try the following code without headless on, works smooth.
Maybe my configuration isnt correct.
Any ideas?
ChromeOptions dCap = new ChromeOptions();
dCap.setHeadless(true);
dCap.setCapability("platform", "LINUX");
dCap.setCapability("version", "latest");
dCap.addArguments("disable-infobars");
dCap.addArguments("--start-maximized");
dCap.addArguments("--disable-extensions");
dCap.addArguments("--disable-gpu");
dCap.addArguments("--window-size=1920x1080");
dCap.addArguments("--enable-javascript");
String driverPath = System.getProperty("user.dir") + "/exe/chromedriver";
System.setProperty("webdriver.chrome.driver", driverPath);
URL rutaProxy = new URL("http://localhost:4444/wd/hub");
WebDriver driver = new RemoteWebDriver(rutaProxy, dCap);
driver.get("https://web.whatsapp.com/");
WebDriverWait espera = new WebDriverWait(driver, 20);
espera.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(".landing-main canvas")));
WebElement canvas = driver.findElement(By.cssSelector(".landing-main canvas"));
JavascriptExecutor js = (JavascriptExecutor)driver;
String imagenBase64 = (String) js.executeScript("return arguments[0].toDataURL('image/png').substring(21);", canvas);
//TEST
byte[] imageByte = Base64.getDecoder().decode(imagenBase64.substring(1));
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
BufferedImage image = ImageIO.read(bis);
bis.close();
File outputfile = new File("image.png");
ImageIO.write(image, "png", outputfile);
driver.close();
I've been in this situation, add more time delay between a action and a window popup, because the refresh of some DOM elements takes time. Enabling headless requires more latency than not enabling it.

How to maximize selenium screen

Would anyone know how to maximize the selenium webdriver window with java and google chrome.
I already tried some commands like maximize () window () and did not work.
This is how I did it.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
Try with this:
String chromeDriver = "/PathTo/chromedriver";
System.setProperty("webdriver.chrome.driver", chromeDriver);
ChromeDriver driver = new ChromeDriver();
//set your max dimension
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension dim= new Dimension((int)screenSize.getWidth(),(int)screenSize.getHeight());
driver.manage().window().setSize(dim);
//Maximizing Chrome Window.
int x=1280; //according to your screen resolution
int y=860; //according to your screen resolution
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("window.resizeTo(x, y);");
You can try too sending keystrokes:
SendKeys(Keys.ALT, " "); // Space, open window menu
SendKeys("x"); // Maximize
Include a pause between keystrokes, although it could work for you without it.
If you're using a Mac you'll need to use the following
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-fullscreen");
WebDriver driver = new ChromeDriver(chromeOptions);
You can do this using the WebDriver API, and this will work with other browsers and RemoteWebDriver:
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
https://stackoverflow.com/a/26998387/5603509
Try following code:
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized"); //This maximizes the window
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);

Exception when using headless Chrome to take screenshot of a video

I'm trying to take screenshots of a video using Selenium and Java. It works fine when I use headed Chrome, but when I want to make it headless a "java.awt.image.RasterFormatException: (y + height) is outside of Raster" is thrown in
getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
Here's the code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
// Get URL and find image
driver.get("https://www.webcamtaxi.com/en/netherlands/north-holland/purmerend-traffic-cam.html");
Thread.sleep(5000);
while (true) {
WebElement ele = driver.findElement(By.id("insideCam"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
}
Does someone know how to fix this?

Categories

Resources