My application will download a file when clicking on a button.
The file will download POST submit and there is no way to get the URL of the file before submitting it.
I am looking for a way to download POST request attachment using selenium. Can someone guide me on how to achieve this using selenium Java?.
I am not certain of your exact use case, but would something like this work? I made this example to download the Google Chrome.exe automatically. Like with your file, there is no direct link to it either but rather just a download generated by the website.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class Main {
public static void main(String[] args) {
ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver","where your ChromeDriver lives");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("download.default_directory", "where you want the file to be downloaded");
prefs.put("download.prompt_for_download", false);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
String baseUrl = "https://www.google.com/chrome/";
driver.get(baseUrl);
try {
TimeUnit.SECONDS.sleep(5);
} catch(Exception e){
}
List<WebElement> targetsWithClass = driver.findElements(By.className("chr-cta__button--blue"));
targetsWithClass.get(1).click();
try {
TimeUnit.SECONDS.sleep(3);
} catch(Exception e){
}
driver.findElement(By.id("js-accept-install")).click();
}
}
Related
I'm new to java. So this is the code i found to open a web page in default browser. I need it to open an incognito window in chrome and then go to the URL.
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
public class OpenWebPage {
public static void main(String[] args) {
try {
URI uri= new URI("https://www.google.com");
java.awt.Desktop.getDesktop().browse(uri);
System.out.println("Web page opened in browser");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I think it can help.
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
See ChromeDriver from WebDriver and this.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class Sel{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32\\chromedriver.exe");
// Initialize browser
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
WebDriver driver=new ChromeDriver(options);
// Open facebook
driver.get("https://www.facebook.com");
driver.manage().window().maximize();
}
}
This being the entire code. Thanks for the help!
I would like to seek help if there is a possible way / code to capture a screenshot under Krypton platform that uses JAVA selenium. I'm having trouble in terms of it's standardization. Thanks!
var driver = new ChromeDriver()
driver.get("https://login.bws.birst.com/login.html/")
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"))
As per the screenshots there seems to be some discrepency with the imports/Classes. Further to keep things simpler create destination folder within the Project Scope e.g. the Screenshots directory below:
To capture a screenshot using Java you can use the following solution:
Code Block:
package screenShot;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class takesScreenshot {
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://login.bws.birst.com/login.html/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("Birst"));
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(".\\Screenshots\\Mads_Cruz_screenshot.png"));
}
}
Captured Image:
I tried the below code for taking full page screenshot. But only the visible area is captured,
public void Fullscreen (WebDriver driver)
{
try {
final Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();
ImageIO.write(image, "PNG", new File("D:\\" + "AShot_BBC_Entire.png"));
} catch(Exception e){
System.out.println(e.getMessage());
}
}
While working with Selenium Java Client v3.14.0, ChromeDriver v2.41, Chrome v 68.0 using ashot-1.4.4.jar here is an example to take the complete page screenshot both horizontally and vertically using ChromeDriver and aShot Library of the url https://jquery.com/:
Code Block:
import java.io.File;
import javax.imageio.ImageIO;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class ashot_CompletePage {
public static void main(String[] args) throws Exception {
System.setProperty("god.bless.you", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(options);
driver.get("https://jquery.com/");
new WebDriverWait(driver, 20).until(ExpectedConditions.titleContains("jQuery"));
Screenshot myScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
ImageIO.write(myScreenshot.getImage(),"PNG",new File("./Screenshots/elementScreenshot.png"));
driver.quit();
}
}
Screenshots:
Reference
You can find a detailed discussion in How to take screenshot with Selenium WebDriver
Want to add answer in case when you don't know what kind of screen is used. (retina or not)
In this case you need find devicePixelRatio of browser window:
Object output = ((JavascriptExecutor) webDriver).executeScript("return window.devicePixelRatio");
String value = String.valueOf(output);
float windowDPR = Float.parseFloat(value);
Then you can use ShootingStrategy with scaling;
ShootingStrategy shootingStrategy = ShootingStrategies.viewportPasting(ShootingStrategies.scaling(windowDPR), 100)
I am trying to capture simple screenshot using phantomjs for https://www.google.com . Screenshot is coming as blank . I am using eclipse in windows and jars phantomjsdriver1.1 , selenium jars all 2.39 version .
When I mention site as http://google , it redirects to https and captures screenshot . But I need it to exclusively capture it for https only . Below is my code . Thanks in advance .
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import net.anthavio.phanbedder.Phanbedder;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Schedule {
public static void main(String[] args) {
File phantomjs = Phanbedder.unpack();
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantomjs.getAbsolutePath());
dcaps.setCapability("takesScreenshot", true);
String [] phantomJsArgs = {"---ignore-ssl-errors=yes"};
dcaps.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
phantomJsArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcaps);
driver.get("https://www.google.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(scrFile, new File("C:\\Users\\Desktop\\fci\\sample.jpeg"),true);
} catch (IOException e) {
System.out.println("exception");
}
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
}
PhantomJS is a headless browser meaning; you don't get to see what is happening; everything that's done is done in background.
I want to export the cookies along with headers to file after a successful login in Selenium, I am able to login using Selenium.
Below is my code which does login in to a website using Selenium
package login;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.logging.Logs;
public class Login1 {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",
"C:/Program Files (x86)/Google/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("url");
driver.manage().window().maximize();
driver.findElement(By.id("username")).sendKeys("namehere");
driver.findElement(By.id("password")).sendKeys("passhere");
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.id("submit")).click();
Logs logs = driver.manage().logs();
LogEntries logEntries = logs.get(LogType.DRIVER);
for (LogEntry logEntry : logEntries) {
System.out.println(logEntry.getMessage());
}
}
}
This is an example with Selenium to save all the cookies to a file:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Path cookiesFile = Paths.get("C:\\Temp\\cookies.txt");
// save the cookies to a file for the current domain
try (PrintWriter file = new PrintWriter(cookiesFile.toFile(), "UTF-8")) {
for (Cookie c : driver.manage().getCookies()) {
file.println(c.toString());
}
}
As for the headers, well Selenium doesn't provide an API to get them.