This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to upload a file from a site using Selenium's java inteface
I am new to Selenium.Can you please tel me how to automate file upload in Internet Explorer using Selenium?
It is not easy, and it is not easy for very good reasons - security. If you are able to upload something like this, what's stopping someone uploading your details using the same method?
You have also given us no example to work with so:
Given this sample webpage:
<html>
<head>
<style type="text/css">
.fileSave { color: red; }
</style>
</head>
<label for="fileUpload">File location:
<input type="file" id="fileUpload" />
<br>
<br>
Save file
</body>
</html>
I can do this, in C#:
Driver = new ChromeDriver();
var fileUploadControl = Driver.FindElement(By.Id("fileUpload"));
fileUploadControl.SendKeys("C:\File.txt");
var submitLink = Driver.FindElement(By.ClassName("fileSave"));
submitLink.Click();
This has been asked several times and is also in some Selenium FAQ.
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");
For Selenium RC, see this question.
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.
Related
I'm writing code to test the facebook website login feature. My chrome browser successfully launches and can enter user and password but I'm unable to select the 'Accept All' button when the test first goes on the website for the cookies.
My code goes onto Facebook.com then tries to find the accept all button. But I can get exception
element not interactable: element has zero size
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id=\"facebook\"]/body")).click();
Below is the output of inspect
<button value="1" class="_42ft _4jy0 _9o-t _4jy3 _4jy1 selected _51sy" data-cookiebanner="accept_button" data-testid="cookie-policy-banner-accept" title="Accept All" type="submit" id="u_0_j_wR">Accept All</button>
//button[.='Accept All']
As well as adding a webdriver wait would solve your issue.
I am using the following code to upload files to a website to a 'file' type element.
The code works fine in Firefox, Chrome and Safari.
However when I run the code against Edge the file is NOT uploaded
driver.setFileDetector(new LocalFileDetector());
selectFile.sendKeys(path);
This error is reported:
The command failed because the specified element is not pointer or keyboard interactable.
If I try using Javascript like this:
document.getElementById('manual_file_selection').sendKeys(path)
I get this: Object doesn't support property or method 'sendKeys'
As stated the same code works fine in Chrome, Firefox and Safari so I don't understand it.
This is the code behind the file upload button:
<div class="jsx-parser">
<div data-xxxxx-element="manual-file-selection">
<div class="button__container">
<label for="manual_file_selection" class="button button--primary" data-dragging="false" data-xxxxx-element="manual-file-selection--label">
<input id="manual_file_selection" type="file" accept="image/jpeg,image/png" data-xxxxx-element="manual-file-selection--input">
<span>Select File</span>
</label>
</div>
</div>
</div>
Anyone had any success uploading files to Edge with Selenium or is it not supported?
Based on your error messages, I'd give some Javascript a try. It's a bit hacky, as we execute JS to reveal the hidden input element, then send keys to it, but I've had success in the past.
// fetch the element
WebElement input = driver.findElement(By.XPath("//input[#type='file']"));
// run JS to reveal the element
JavascriptExecutor executor = (JavaScriptExecutor)driver;
executor.executeScript("arguments[0].style.display = 'block';", input);
// send file path keys
input.sendKeys(path);
It's worth a try. Let me know if this helps at all.
I am using PhantomJS in selenium for the driver. Here, I find that there is no problem inserting the email address. However, after clicking the next, there should have a input tag named 'password'. But, after waiting, I can not get the required 'password' tag. It shows error not finding the element. Sometimes, I am getting the following error (showing stacktrace):
org.openqa.selenium.InvalidElementStateException: {"errorMessage":"Element is not currently interactable and may not be manipulated","request":{"headers":{"Accept-Encoding":"
Since, I have waited enough time, the following code must work.
Here is the below code snippet for working.
driver.get("https://accounts.google.com/ServiceLoginAuth");
driver.findElement(By.id("identifierId")).sendKeys("xxxxxxxx");
driver.findElement(By.id("identifierNext")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.name("password")));
driver.findElement(By.name("password")).sendKeys("xxxxxxxx");
driver.findElement(By.id("passwordNext")).click();
Here is the specific html showing the input field from the google sign in page for email:
input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="username" spellcheck="false" tabindex="0" aria-label="Email or phone" name="identifier" id="identifierId" dir="ltr" data-initial-dir="ltr" data-initial-value=""
The id of Next button is 'identifierNext' and hhe hidden password field:
input type="password" name="hiddenPassword" jsname="RHeR4d" class="yb9KU" tabindex="-1" aria-hidden="true"
After inserting email and click on the next, the input field for the password is:
input type="password" class="whsOnd zHQkBf" jsname="YPqjbf" autocomplete="current-password" spellcheck="false" tabindex="0" aria-label="Enter your password" name="password" autocapitalize="off" autocorrect="off" dir="ltr" data-initial-dir="ltr" data-initial-value=""
Now, the question is, I have used waiting mechanism for reloading the page so that I can get the password insertion page. But, using Selenium I can not find the named tag of 'password'.
I think the click is not working properly. Is there any chance of not working the click on Next?
Try this:
driver.findElement(By.xpath("//input[1]"));
Or this:
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[#type='password']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#type='password']")));
WebElement elementpwd = driver.findElement(By.xpath("//input[#type='password']"));
I hope it helps.
Please find the full code for sending email for Gmail using Selenium Web Driver using Chrome browser
public class Newtest {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Tharun\\Desktop\\work\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.google.com");
driver.findElement(By.xpath("//*[#id='gb_70']")).click();
driver.findElement(By.xpath("//*[#id=\'identifierId\']")).sendKeys("*******#gmail.com");
driver.findElement(By.xpath("//*[#id=\'identifierNext\']/content/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//input[#type='password']")).sendKeys("*******");
driver.findElement(By.xpath("//*[#id=\'passwordNext\']/content/span")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#id=\"gbw\"]/div/div/div[1]/div[2]/a")).click();
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#id=':ir']/div/div")).click();
Thread.sleep(5000);
driver.findElement(By.cssSelector("#\\3a og")).sendKeys("*******#gmail.com");
driver.findElement(By.cssSelector("#\\3a ny")).sendKeys("This is my selenium web driver automation code ");
Thread.sleep(5000);
driver.findElement(By.xpath("//*[#id=\':p3\']")).sendKeys("This is my selenium web driver automation code ");
Thread.sleep(2000);
driver.findElement(By.xpath("//*[#id=\':no\']")).click();
}
}
I am running a selenium webdriver script headless using Phantomjs Driver. I am having issues uploading a file though since on a normal browser (firefox or chrome) it would pop up the OS dialog box that would allow me to locate the file in my machine and upload it.
How to do that with the ghostDriver (Phantomjs Driver)?
Thanks
Always identify & interact with elements of type "file" when uploads are concerned. This would solve your issue of pop ups.
Ex: In my application, upload related elements have the below DOM -
<a id="uploadFileButtonLink" class="uploadFileButtonLink" href="javascript:void(0)" data-uidsfdc="3" style="display: none;">Upload a file</a>
<input id="multiFileInput" class="multifile-upload-input-button" type="file" name="chatterFile_upload" multiple="multiple"/>
<input id="multiUploadBtn" class="btnImportant" type="button" value="Upload Files"/>
In this case, you can use sendKeys method to "multiFileInput" which is of type "file".
This way it would work for all FF, Chrome & also headless browsers.
I am having the same issue and have posted a question for the same. PhantomJS hangs up when using sendKeys() method.
They have an issue logged here - https://github.com/ariya/phantomjs/issues/10993
One of the comments on the issue stated that the below statement worked -
(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");
You may try the above solution, but it may or may not work.
This code helped me with uploading if 'multiple' attribute was set:
protected void uploadFile(CharSequence... keys) {
if (((WrapsDriver) driver).getWrappedDriver() instanceof PhantomJSDriver) {
StringBuffer s = new StringBuffer(keys.length);
for (int index = 0; index < keys.length; index++) {
s.append(keys[index].toString());
}
((PhantomJSDriver) ((WrapsDriver) driver).getWrappedDriver()).executePhantomJS(
String.format("var page = this; page.uploadFile(arguments[0], '%s');", s.toString()), getElement());
} else {
getElement().sendKeys(keys);
}
}
var webPage = require('webpage');
var page = webPage.create();
page.uploadFile('input[name=image]', '/path/to/some/photo.jpg');
in the new version of phantomjs, you can upload file like this
uploadfile
I am using simple extjs prompt for getting one value from user
Ext.Msg.prompt('Name', 'Please enter your name:', function(btn, text){
if (btn == 'ok'){
// process text value and close...
}});
In chrome it is rendering correctly, but in ie7 and Firefox, the text-box stretches completely without any padding. so that i cant see left & right side of the text-box.
Thanks in Advance.
For IE7 copy this code into your HTML file: (<head> Tag)
<!--[if IE 7]>
<style type="text/css">
.x-window-body-default .x-form-text {
width: 215px !important;
}
</style>
<![endif]-->
We don't have this problem in FireFox. Which version of FireFox are you using? If you are using the older versions, you can use <style> above that only targets FireFox with help of the link below depending on your version:
http://css-tricks.com/snippets/css/css-hacks-targeting-firefox/