Could you please let me know how I can upload one pdf from my system into the website. Here is the example of my code, but its not working :
Driver.get("https://www.pdfunlock.com/");
Driver.manage().timeouts().implicitlyWait(1,TimeUnit.MINUTES);
Driver.findElement(By.id("fromComputer")).click();
Driver.findElement(By.id("Open")).click();
WebElement = Driver.find_element_by_id("fileUpload")
element.send_keys("C:\myfile.txt")
Please help.
Thankiew.
Selenium WebDriver operates only on browser DOM window. When you are trying to upload a file, you are intending to automate a windows level flow, which is out-of-scope for selenium. In short, you cannot use selenium in any form to upload a file.
But... you can do so, using Java's Robot API, or using an AutoIT script.
Please visit this link to learn more about AutoIt, and file uploading using it.
http://toolsqa.com/selenium-webdriver/autoit-selenium-webdriver/
To use the compiled AuoIT script in your Java code, simply use this
Runtime.getRuntime().exec(pathToTheExecutableFile);
Use Robot API to interact with Windows pop-up or Browse windows . Selenium will not be able to interact with Windows file browser.
Go through this for quick overview:
Reference Link
I am assuming you are not able to upload because you need to browse to your local location to upload to your website
try this (Only Drawback of this is you have to be on the opened browser open dialog when this code is running i.e. this code won't run in background) :
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Lists {
public static void main(String[] args) throws AWTException {
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumDriver\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.pdfunlock.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='fromComputer']")).click();
String filePath = "C:\\Users\\kushal\\Desktop\\1.pdf";
StringSelection stringSelection = new StringSelection(filePath);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, stringSelection);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
}
}
You can use following code (Robot API) to upload a file to website, if you are using java.
//After Open Upload window
//Copy the file path in clipboard
StringSelection ss=new StringSelection("File path");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
//Paste your copied path in modal window using mouse events
Robot rb=new Robot();
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);
Related
I am currently working on the selenium+java test automation project.
I have encountered a problem that while running many test classes together in testng.xml, firefox display
confirm resubmission popup. when I am running a single class this message does not popup. This confirmation popup comes when the last set of classes begins to run. How can I resolve this problem?
May be, you can try to simulate pressing Enter key or Spacebar and see if that works.
import java.awt.Robot;
import java.awt.event.KeyEvent;
public void pressEsc(WebDriver driver) {
try {
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
} catch (Exception e) {
e.printStackTrace(); }
}
Similar question asked below
How to save complete web page
But there is no answer yet. The expected result is to get many files, some file to store image, etc.
I used the following,it will pop up a windows saying to save the file
val a=new FirefoxDriver()
a.get("http://www.baidu.com")
val b=new Actions(a)
b.action.keyDown(Keys.ALT).keyDown(Keys.F4).keyUp(Keys.ALT).perform();
But then how to click the save button? The following doesn't work
b.sendKeys(Keys.ENTER)
We can use Robot utility in Java to handle this:
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
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);
Thread.sleep(2000L);
// press Enter
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
To use Robot utility you have to import following Java utilities:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
When you use Robot utility the x axis and y axis has to be metioned appropriately however it will differ system to system instead we can use AUTO IT.
You can refer here http://www.autoitscript.com/forum/forum/9-example-scripts/
Please help me in saving web page as image using java.
I am using selenium web driver for an application, I need to take screenshot for an alert box.
So I thought it will be better if we have "save as image" button so that I can take the alert screenshot.
I am using firefox web driver
Newer versions of firefox having new feature of executing commands by pressing SHIFT+F2
It helped me in taking alert screenshots, I used robot object for this but not using web driver
You can simply install Firefox plugin: https://addons.mozilla.org/en-US/firefox/addon/fireshot/ and take any screenshot of web page.
This robot function will help you to take screenshot of displaying screen. You can edit it.
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
public void captureScreen(String fileName)throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = newRectangle(screenSize);
Robot robot = newRobot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image,"png",newFile(fileName));
}
The following may help you.
File screenShot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
File f = new File("Location to save your image ");
I am using Selenium Webdriver(Java), i want to use IE driver for my testing, however i come up with the problem, can anyone help me out of this please, The script which are running fine in firefox fails to run in IE, I am just opening a google page and searching some word but my code only opens the google page write the keyword but unable to hit the serch button on google page using IEdriver, after too much google i found one thimg that when IE browser get opens it will opened in IE8 Compatibility view and due to this its attributes like id, name get changed as compared to FF, but when i changes this to IE8 view manually the properties are same as FF,(Press F12 key on keyboard open developers tool on IE) So can anyone please let me know how to overcome this or how to open the IE browser in IE8 mode, or anyone knows any different solution of using IE for selenium webdriver.
My code is as follows
package backOffice;
import java.io.File;
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.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.gargoylesoftware.htmlunit.javascript.configuration.BrowserName;
import bsh.ParseException;
public class Time
{
private WebDriver driver;
private String baseUrl= "http://www.google.co.in/";
public static void main(String args[]) throws InterruptedException
{
Time tm=new Time();
tm.trial();
}
private void trial() throws InterruptedException
{
File file = new File("C:/Documents and Settings/Administrator/Desktop/32- bit_IEDriverServer_Win32_2.31.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer(); caps.setCapability("ignoreZoomSetting", true);
driver=new InternetExplorerDriver(caps);
driver.get(baseUrl + "/");
driver.findElement(By.id("gbqfq")).clear();
driver.findElement(By.id("gbqfq")).sendKeys("harshal kakade");
driver.findElement(By.id("gbqfb")).click();
driver.findElement(By.linkText("Harshal Kakade - India | LinkedIn")).click ();
}
}
Thanks,
Harshal.
try to look at the internet options -> security and uncheck "Enable Protected Mode". Probably there is the problem.
I saw that lots of people have Problems uploading a file in a test Environment with Selenium WebDriver. I use the selenium WebDriver and java, and had the same problem. I finally have found a solution, so i will post it here hoping that it helps someone else.
When i need to upload a file in a test, i click with Webdriver in the button and wait for the window "Open" to pop. And then i copy the path to the file in the clipboard and then paste it in the "open" window and click "Enter". This is working because when the window "open" pops up, the focus is always in the "open" button.
You will need these classes and method:
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
public static void setClipboardData(String string) {
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
}
And that is what i do, just after opening the "open" window:
setClipboardData("C:\\path to file\\example.jpg");
//native key strokes for CTRL, V and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
And that´s it. It is working for me, i hope it works for some of you.
Actually, there is an in-built technique for this, too. It should work in all browsers and operating systems.
Selenium 2 (WebDriver) Java example:
// assuming driver is a healthy WebDriver instance
WebElement fileInput = driver.findElement(By.xpath("//input[#type='file']"));
fileInput.sendKeys("C:/path/to/file.jpg");
The idea is to directly send the absolute path to the file to an element which you would usually click at to get the modal window - that is <input type='file' /> element.
Thanks Alex,
Java Robot API helped me for uploading file. I was fedup with File Upload using WebDriver. Following is the code I used (Small modification to yours):
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.delay(1000);
Thanks Alex! I needed this since I could not get the sendKeys function to work when used via Play framework 2.1 (fluentlenium wrapper). I am testing over Firefox [17.0.7] for Mac and had to make a few mods to get it working. Below is an approximation of the final snippet I used.
val file = new File(PATH_TO_IPA)
val stringSelection: StringSelection = new StringSelection(file.getAbsolutePath)
Toolkit.getDefaultToolkit.getSystemClipboard().setContents(stringSelection, null)
val robot: Robot = new Robot()
// Cmd + Tab is needed since it launches a Java app and the browser looses focus
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_TAB)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_TAB)
robot.delay(500)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_SHIFT)
robot.keyPress(KeyEvent.VK_G)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_SHIFT)
robot.keyRelease(KeyEvent.VK_G)
robot.keyPress(KeyEvent.VK_META)
robot.keyPress(KeyEvent.VK_V)
robot.keyRelease(KeyEvent.VK_META)
robot.keyRelease(KeyEvent.VK_V)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
robot.delay(500)
robot.keyPress(KeyEvent.VK_ENTER)
robot.keyRelease(KeyEvent.VK_ENTER)
The switching of application on Mac is much better to do with AppleScript. AppleScript is integrated to system, so it will be always functional and does not depend on order of apps on Cmd+Tab. Your test/app will be less fragile.
https://developer.apple.com/library/content/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_cmds.html
You need only detect you are on mac and has name of the application.
Runtime runtime = Runtime.getRuntime();
String[] args = { "osascript", "-e", "tell app \"Chrome\" to activate" };
Process process = runtime.exec(args);