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.
Related
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");
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);
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?
Hi all IM having troubles. My test click an hyperlynk and a new window is displayed. the problem is that when I implement windows handlers selenium only identify one window instead of 2 window and when I try to find the element of the window, well I cant cause for selenium the window never exist :(
driver.switchTo().frame(CQLo.getMframe());
WebElement Rad_CIT = (new WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(CQLo.getRadCIT()));
Rad_CIT.click();
WebElement Text_CIT = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getTextCIT()));
Text_CIT.clear();
Text_CIT.sendKeys(citbox);
setLog("CITBox: " +citbox);
//WebElement Link_WFM =
Actions act = new Actions(driver);
WebElement onElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getLink_WFM_Admin()));
act.contextClick(onElement).perform();
act.sendKeys("o").perform();
Set <String> wind_ows = driver.getWindowHandles();
for(String sw : wind_ows)
{ System.out.println(""+sw.toString());}
setLog("Open log on " +citbox);
Thread.sleep(2000);
WebElement Text_UserN = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserName()));
Text_UserN.sendKeys(MSR_name);
WebElement Text_UserP = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserPassword()));
Text_UserP.sendKeys(MSR_pass);
WebElement B_logon = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Elog.getB_logon()));
B_logon.click();
after printing the SET with the windows I only receive one window
Wait some time before you get window handles after performing click on element. I think by the time you read driver window handles, new window might not be opened.
Am trying to save .html page into my desktop using selenium .
Can anyone pls help?
You can use the Advanced User Interactions API:
new Actions(driver)
.sendKeys(Keys.chord(Keys.CONTROL, "s"))
.perform();
This will open the Save As dialog in which you then need to navigate manually / using the Java's Robot class:
Robot robot = new Robot();
// press Ctrl+S the Robot's way
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_S);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_S);
// press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Presses Ctrl+S, Enter. Beware that will not work on every system and/or browser.
JavascriptExecutor js = (JavascriptExecutor) driver;
String base64string = (String) js.executeScript("var c = document.createElement('canvas');"
+ " var ctx = c.getContext('2d');"
+ "var img = document.getElementsByTagName('img')[0];"
+ "c.height=img.naturalHeight;"
+ "c.width=img.naturalWidth;"
+ "ctx.drawImage(img, 0, 0,img.naturalWidth, img.naturalHeight);"
+ "var base64String = c.toDataURL();"
+ "return base64String;");
String[] base64Array = base64string.split(",");
String base64 = base64Array[base64Array.length - 1];
byte[] data = Base64.decode(base64);
ByteArrayInputStream memstream = new ByteArrayInputStream(data);
BufferedImage saveImage = ImageIO.read(memstream);
ImageIO.write(saveImage, "png", new File("path"));