I am trying to upload a file from a specific path in Python.
video_path = 'C:\\Users\\x\\Desktop\\s.mp4'
self.driver.find_element_by_xpath(file_upload).send_keys(video_path)
This works, but it opens this:
I have tried several xpaths, including ones with the word 'input' or other. I tried using this: https://www.tutorialspoint.com/how-to-upload-file-with-selenium-python
Here is screenshot of all the xpaths:
<input type="file" name="Filedata"
button id="select-files-button" type=" (This is the button that press that opens the select file option.)
Related
I'm trying to upload a file to a site. Everything is fine, but at the moment selenium tries to read data from tag, uploading suddenly stops.
The uploading progress bar html code is:
<div id="progressbar1" class="progressbar">
<div id="progresspercent1" class="progresspercent">5%</div>
</div>
and my code is:
bar_id = "progresspercent1"
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, bar_id)))
print("PERCENT:" + driver.find_element(By.XPATH, bar_id).text)
I also tried accessing this tag:
<tr id="uploadfile1" title="Upload" uploadrowid="1" status="cancel">
</tr>
And I used its "status" attribute to check if the file is uploading or not, but I had the same problem again. And I also tried using get_attribute('innerHTML') method (for progress bar) but that doesn't work too.
EDIT:
This is how I start uploading:
driver.find_element(By.ID, "menu_upload").click()
frame = driver.find_element(By.XPATH, '//*[#id="uploader1"]')
driver.switch_to.frame(frame)
driver.find_element(By.ID,"fileupload").send_keys(<File Address>)
The first line opens the upload box frame. I just switch to the frame and then try to upload the file using the last line.
And right now I'm not doing anything after printing the percent. Later on I might create a loop to print the percent and when the file was uploaded, close the webdriver.
I am trying to automate a few file uploads with selenium and python.
Everything works fine the first file upload but when i try to upload the second file it just does not work. It does not give me errors and when i rerun the script the first upload works fine. The website has a file upload like this:
<input class="upload-class" type="file" accept="image/*" aria-label="upload text">
I use something like this:
driver.find_element(By.XPATH,"//input[#class='upload-class']").send_keys("C:\\path\\to\\file.jpg")
I am trying to upload a file using python automation.
While I try to execute the code below python selenium throws an error.
Even I tried waiting for 10 seconds to avoid synchronisation issues.
driver.execute_script('window.open("https://ocr.space/" , "new window")')
Imagepath = r"C:\User\Desktop\banner.png"
field=driver.find_element_by_xpath('//input[#type="file"]')
field.send_keys(Imagepath)
NoSuchElementException: Message: no such element: Unable to locate
element: {"method":"xpath","selector":"//input[#type="file"]"}
Website url:
https://ocr.space/
HTML snippet:
<div class="span8">
<input type="file" id="imageFile" class="form-control choose valid">
</div>
Changing the code to launch the url with get seems to solve the issue.
from selenium import webdriver
driver = webdriver.Chrome("./chromedriver")
driver.get("https://ocr.space/")
image = r"C:\Users\Thanthu Nair\Desktop\soc360.png"
field=driver.find_element_by_xpath('//input[#type="file"]')
field.send_keys(image)
Also make sure the path provided C:\User\Desktop\banner.png is correct, otherwise you'll get another exception. It is just my assumption that this path might be wrong because usually Desktop folder is inside folder with user's name which is inside the User folder. In this case you've Desktop folder is inside User folder according to the path you've give.
To solve your problem, simply replace new window with _self in the below line of your code :
driver.execute_script('window.open("https://ocr.space/" , "_self")')
Your code is working fine but the reason for an error is, after running your code it launches browser with two tabs nothing but windows and the page will be launched in the second window so you need to switch to that window before uploading an image.
You can use window handles for switching to that window. Below is the code in Java, you can try doing same using Python :
// Using JavaScriptExecutor to launch the browser
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open(\"https://ocr.space/\" , \"new window\")");
// Fetching window handles and switching to the last window
Set<String> handles = driver.getWindowHandles();
for(String handle : handles) {
driver.switchTo().window(handle);
}
// Printing window title
System.out.println(driver.getTitle());
// Uploading an image
WebElement field = driver.findElement(By.xpath("//input[#type='file']"));
String imagePath = "some image";
field.sendKeys(imagePath);
If you use window.open() to launch an URL then it will do two things, first it will launch browser with default window then it will open URL in new tab even if you don't provide new window argument in your JavaScript function. You need to switch to a particular window to perform any operations on it if you choose this way.
To avoid an above problem, simply you can use driver.get(URL) or driver.navigate().to(URL) which launches the browser and navigates to a particular URL in the same launched browser window.
If you want to use JavaScriptExecutor only without doing switching, you can pass _self as a second argument to the JavaScript function like below instead of new window which avoids switching and launches an URL in the same window :
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open(\"https://ocr.space/\" , \"_self\")");
System.out.println(driver.getTitle());
WebElement field = driver.findElement(By.xpath("//input[#type='file']"));
String imagePath = "some image";
field.sendKeys(imagePath);
I hope it helps...
Generally, when the file upload related <input> tag contains the attribute type as file you can invoke send_keys() to populate the relevant text field with a character sequence. However, in your usecase the <input> tag though having type="file" but the class attributes are form-control choose which is as follows:
<input type="file" id="imageFile" class="form-control choose">
So, you may not able to able to send a character sequence invoking send_keys().
In these cases you need to use Auto IT based solutions. You can find a couple of relevant discussion in:
How to upload a file in Selenium with no text box
How can I click file upload page options with python selenium? I mean, I click this <input type="file" id="uploadfile"> with this code: driver.find_element_by_id("uploadfile").click()
then, a small window opens, this window's title is 'File Upload' and I want write on location input in this window. How can I do this? Thanks !
If the file upload option is an input tag and its type is file then you can directly upload the file using send_keys() method in Selenium
e.g.
driver.find_element_by_id("uploadfile").send_keys("D:\test\filename.extension")
Note :enter absolute location of your file
I am trying to click a button that brings up a dialogue box to select a file. inspecting the element it looks like its an input rather than a button. Either way I cannot click it with:
element = browser.find_element_by_id("fileupload")
element.click()
and
browser.find_element_by_id("fileupload").send_keys("\n")
Neither of which seem to work.
Here is what I see when I inspect that element on the page:
<span class="btn btn-success fileinput-button">
<span class="glyphicon glyphicon-upload"></span>
Select and Upload...
<input id="fileupload" name="upfile" accept=".xml" type="file">
</span>
Any assistance help or pointers would be appreciated!
Clicking on a file input usually triggers a file upload dialog. Since you cannot control it with selenium, you need to avoid the dialog being opened by sending keys to the input instead:
browser.find_element_by_id("fileupload").send_keys("path_to_the_file")
See also:
How to deal with file uploading in test automation using selenium or webdriver
How to upload file using Selenium WebDriver in Java
How to upload file ( picture ) with selenium, python