I am using selenium to upload a file. This is a problem because the upload button makes a file explorer window appear which I can't automate to my knowledge. How can I either automate file explorer or upload the file directly from selenium? It should select a specific file from a path to upload.
Any help is greatly appreciated.
This is my code:
upload1 = driver.find_element_by_xpath(upload1xpath)
upload1.send_keys('C:\Users\bodig\Downloads\image1.jpg')
To upload a file without clicking the upload button in most cases you can send your file directly to some element on that page.
This will be element with input tag name with attribute type='file'. Not the button user clicking but somewhere near it.
So you can try finding that element with this:
upload_input = driver.find_element_by_xpath('//input[#type="file"]')
upload.send_keys('C:\Users\bodig\Downloads\image1.jpg')
If you prefer using css_selector you can use this:
upload_input = driver.find_element_css_selector('input[type="file"]')
upload.send_keys('C:\Users\bodig\Downloads\image1.jpg')
To find specific element on your page I have to see the web page
Related
I want to upload a csv file to a google sheet at a particular cell. Since I need to upload a csv file to a certain cell I thought I would try to use selenium. I have a cell selected and the upload pop-up opened. But whenever I try to "send_keys" I get the "element not interactable" error. Is it possible that the upload element of type file is hidden?
My code:
#open upload popup
cell_location.send_keys('\ue03do')
#get iframe of upload popup
iframe_path = driver.find_element_by_xpath('/html/body/div[{}]/div[2]/iframe'.format(141))
driver.switch_to.frame(iframe_path)
time.sleep(1)
#navigate to upload file
upload = driver.find_element_by_xpath('//*[#id=":9"]/div')
upload.click()
time.sleep(1)
select_file = driver.find_element_by_xpath('//*[#id=":1f"]/div')
#pathetically attempt to upload file
time.sleep(1)
select_file.click()
select_file.send_keys('/Users/Documents/web_scaper/grant_proposals.csv')
Please let me know if anything is unclear! Thank you!
I've been trying a lot of different things to make this work somehow. Finally found the solution.
You're not supposed to click on the "Upload" button itself, underneath it, you will see a input with inspect element.
Just copy the XPath of that input element, that is where you do .send_keys("file path").
Here's the example:
driver.find_element("xpath", '//*[#id="yDmH0d"]/div[2]/div[3]/div[2]/div[2]/div/div/div/div[1]/div/div[2]/div/input')
I have been trying to access the elements (input ID, button ID etc.) of a File Upload dialogue window using Selenium in Python, with no success. The File Upload window seems to be a Windows dialogue window. Have already tried the different solutions posted on this portal but nothing is working. Please assist.Attached is the screenshot of the File Upload window
A webdriver controls remote sessions on browsers. We cannot modify the windows system file dialog with a webdriver.
Thus, let me enlighten you with a possible solution (it may not be exactly what you want, in which case we will iterate on the solution):
To popup a file dialog to upload a file, you probably would've needed to click on the following html. In human readable terms, it's a file input field.
<input type="file">
But actually, there's no need to directly click on it. You can simply find the element, and send the path of the file to upload.
pathOfFile = "C:/Users/John/yourPotatoIsTwoPotatoes.png"
element = driver.find_element_by_css_selector("input[type='file']")
element.send_keys(pathOfFile)
I'm not sure if you'll like the idea of passing a path name to the file input, so tell me if I've answered your question enough!
I am trying to create a calendar for a webpage with custom information on it. I don't have a strong enough background in css to build a decent calendar, so I turned to the Google embedded calendar. My webpage has a calendar link to my account, but I want to import my own ical file. This file is generated automatically in a script, and I want to add functionality to import the .ics. I currently use selenium to get to the page (https://calendar.google.com/calendar/r/settings/export?tab=wc) but I can't get selenium to capture the window where I select my file. My next idea was to write the file path to the input section then click the import button but the import tag is hidden and I can't seem to find a way to get selenium to find it. My next solution was to use requests.post, but there isn't a clear url to post to. I am stuck, so help with any of the four problems would be much appreciated!
Problems:
1) a decent calendar with event information that is easy to code in css and html or a calendar that can be embedded and accepts .ics
2) a way to capture the file explorer window to type my own path into
3) find hidden input tag to put file path into (and how to make sure the file is uploaded)
4) find the url of the server that the POST method calls
While I can't answer all 4 of your questions, I can help with #3:
# find the input
input = driver.find_element_by_xpath("//input[#type='file']")
# execute script to reveal hidden element -- this part might be optional
driver.execute_script("arguments[0].style.display = 'block';", input)
# send file path keys to the input
input.send_keys("C:/Path/To/Your/File")
# verify if the upload was successful
uploaded_file = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//input[#type='file']/preceding-sibling::span[not(contains(#aria-hidden, 'true'))]")))
if (uploaded_file.text == file_name): # file_name is the name of file, like Test.txt
print("File upload was successful")
This code will locate the input element which accepts your file upload, and execute some Javascript to reveal the element -- this works around any ElementNotVisible or ElementNotInteractable exceptions we may encounter. Then, we send_keys to the input element -- it accepts your full, absolute file path to the item you are trying to upload.
Then, we verify the upload, by checking the span element which appears, containing the name of the uploaded file. If this element exists, and its .text attribute equals the name of the file you uploaded (like Test.txt), then the upload was successful.
uploaded_file.text returns Select file from your computer if the file was not uploaded. This text changes to Test.text when the file has been uploaded successfully.
Hope this helps a bit.
After some research on my problem, it seems I should use either requests or urllib or both.
So basically, I am trying to learn the code I need to download a csv file from this url:
https://globalaccess.sustainalytics.com/#/tools/0
The way I manually download my files is as follows: first, I need to log in using username and password. Next I have to go to a tab called "Screening" that takes to me another page that has several buttons called "Generate". I click a specific generate button (it's always the same one) among the option to get the excel file. After that I have the option to save the file or open from a little window within the website.
My question is what code can I use on Python to download and save the file in a particular folder?
Use Selenium
https://selenium-python.readthedocs.io/
You'll need to download a 'chromedriver' to the same directory as your python script, then use the intro tutorial on the selenium docs site to drive the browser to type/click where you want.
If you use chrome you can right click on any given link/input box click inspect, then in the window that comes up right click the bit of highlighted code and 'copy xpath'. Use the find element by xpath function in Selenium to send keys or clicks to that element.
I'm using python and selenium chrome driver to click on an upload file button which opens an open file window as shown:
My code uploads the files without interacting with the window. What I'm trying to do is close this window but am not sure how to go about doing this.
I've read various other posts with the similar problem but none I could find really gave me what I was looking for. I understand selenium can't access the window, and I've read I need a different module to interact with it. What would be the best way to do this?
In the end I was able to upload the item directly into the page by finding the upload button and sending it the file path.
driver.find_element_by_xpath("//*[#id='upl-fileInp']").send_keys("C:/Users/user/folder/file.jpg")
Workaround
You need put inside element of upload the root file. Don't open the upload by button, just insert /root/of/file/file.pdf inside input upload value using Javascript. Remember, that's my workaround... haha