I have tried to upload a photo using the code
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");
But the image is not getting uploaded.
The html of the upload button is
<button id="upfile1" class="buttonclass" style="cursor: pointer" type="button"> Choose Photo</button>
Is there any other way to upload image. I have tried using WebElement also. I need a solution in JAVA.
I was able to do it using
driver.findElement(By.id("upfile1")).click();
Thread.sleep(2000);
StringSelection ss = new StringSelection("C:\\Users\\logo1.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
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_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
But is there any other simple methods to achieve the same other than using robot?
Add System.Windows.Forms reference and try the below code.
driver.findElement(By.Id("upfile1")).Click();
System.Windows.Forms.SendKeys.SendWait("C:\\Users\\path\\ben.jpg");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
The pop-up which appears is a windows one.It should be handled using AutoIT.
If you are using RemoteWebDriver, then you need to use LocalFileDector to upload the file to the remote selenium server first. And then use the remote path to upload from the remote selenium server.
driver.setFileDetector(new LocalFileDetector());
driver.findElement(By.xpath("xpath")).sendKeys("C:\\Users\\path\\ben.jpg");
Related
While trying to upload file in selenium scripts am getting the error "Unable to determine type from: H. Last 1 characters read: H" on execution through Firefox. But in chrome it works fine.
Hence i tried with Robo class
StringSelection stringSelection = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
waitABit(4000);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
waitABit(6000);
This is not supporting on execution through Browser Stack.
Can anyone help on providing a sample code that support file upload in all browsers and all platforms?
I am trying to automate a page which has an Active directory authentication. I am using Robot class to pass the user/Password which is not working.
I tried debugging and it seems like the Robot class commands are not working because the page is still loading in the background when the AD Authentication window pops up.
Same code works fine when I replace the URL with Google.com portal.
Request your help, Thanks.
Here's the code...
driver.get("https://www.mypageurl.com");
//driver.get("https://www.google.com");
//none of the below line of codes are not executed for mypageurl but works for Google
Thread.sleep(3000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//Code to enter username
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
//Code to enter password
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Try JavaScript executor to wait for page to load.Please see below link hope this help you.
https://www.testingexcellence.com/webdriver-wait-page-load-example-java/
I'm trying to open a new tab in same browser but it doesn't seem to work. I'm using Chrome Version 58.0.3029.110 (64-bit) and Selenium 3.0.0.
I used the below code:
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t");
try using JavascriptExecutor as below:
((JavascriptExecutor) driver).executeScript("window.open('https://www.google.com');");
You can also use Robot class with Selenium Webdriver to open a new tab. We need to follow the below three steps-
Simulate pressing of Ctrl+t keys of keyboard using Robot class.
Switch to the new tab in selenium using driver.switchTo() command.
Open the desired link on new tab.
Code snippet-
//Launch the first URL
driver.get("http://www.google.com");
//Use robot class to press Ctrl+t keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_T);
//Switch focus to new tab
ArrayList<String> tabs = new ArrayList<String (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
//Launch URL in the new tab
driver.get("http://google.com");
Source: Code snippet from Open a new tab in Selenium - ArtOfTesting
Here it is the AutoIT script
ControlFocus("File Upload","","Edit1")
ControlSetText("File Upload","","Edit1", "file path")
ControlClick("File Upload","","Button1")
And Robot script is:
StringSelection stringSelection = new StringSelection(string);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
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);
I just tried with the above scripts in jenkins execution but it is not working.
Can anyone please suggest me with the proper execution script.
If your using Selenium as you've tagged the question, the proper way to upload a file is using sendKeys, here's an example:
string File = "SomeTextFile.txt";
string FilePath = #"C:\Whatever\" + File;
driver.get("http://the-internet.herokuapp.com/upload");
driver.findElement(By.id("file-upload")).sendKeys(FilePath);
driver.findElement(By.id("file-submit")).click();
Your script for Jenkins could be failing for multiple reasons you have to watch it when it happens. Could be your clicking a wrong button, or you don't have an active desktop in your slave etc...
I need to do screenshot of full page using chrome driver, but it makes it partly.
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
The screenshot looks as visible rectangle with correct information and big black area below.
This is a known bug: https://code.google.com/p/chromedriver/issues/detail?id=294 (Only for Chrome driver, firefox driver works fine)
Might worth trying to use this library:
https://www.assertthat.com/posts/selenium_shutterbug_make_custom_screenshots_with_selenium_webdriver
To make a full page screenshot:
Shutterbug.shootPage(driver, ScrollStrategy.BOTH_DIRECTIONS).save();
(it uses scroll-and-stitch method)
Sources on github https://github.com/assertthat/selenium-shutterbug
Provides ability to make full page screenshot in Chrome and some other extended features, tested on Windows and OS X.
Successfully using on my current project.
you need to use
load html2canvas.js
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://github.com/niklasvh/html2canvas/releases/download/0.5.0-alpha1/html2canvas.js';
document.head.appendChild(script);
Command to download full page screenshot by this command
html2canvas(document.body).then(function(canvas) {
var a = document.createElement('a');
// toDataURL defaults to png, so we need to request a jpeg, then convert for file download.
a.href = canvas.toDataURL("image/jpeg").replace("image/jpeg", "image/octet-stream");
a.download = 'somefilename.jpg';
a.click();
})
you may call this script using javascriptexecutor and get desired results as download of the image would launch automatically to your default download location and you may change file name with an input argument of the javascriptexecutor command of the selenium.
hope this helps!
I know this is an old thread, but I wanted to show use of Selenium's ITakesScreenshot.
using OpenQA.Selenium;
using System.Drawing.Imaging;
((ITakesScreenshot)driver).GetScreenshot().SaveAsFile(#"YourImageNameHere.png", ImageFormat.Png);