0

I am taking a trial website case to learn to upload files using Python Selenium where the upload window is not a part of the HTML. The upload window is a system level update. This is already solved using JAVA (stackoverflow link(s) below). If this is not possible via Python then I intent to shift to JAVA for this task.

BUT,

Dear all my fellow Python lovers, why shouldn't it be possible using Python webdriver-Selenium. Hence this quest.

Solved in JAVA for URL: http://www.zamzar.com/ Solution (& JAVA code) in stackoverflow: How to handle windows file upload using Selenium WebDriver?

This is my Python code that should be self explanatory, inclusive of chrome webdriver download links.

Task (uploading file) I am trying in brief: Website: https://www.wordtopdf.com/

Note_1: I don't need this tool for any work as there are far better packages to do this word to pdf conversion. Instead, this is just for learning & polishing Python Selenium code/application.

Note_2: You will have to painstakingly enter 2 paths into my code below after downloading and unzipping the chrome driver (link below in comments). The 2 paths are: [a] Path of a(/any) word file & [b] path of the unzipped chrome driver.

My Code:


from selenium import webdriver
UNZIPPED_DRIVER_PATH = 'C:/Users/....' # You need to specify this on your computer

driver = webdriver.Chrome(executable_path = UNZIPPED_DRIVER_PATH)

# Driver download links below (check which version of chrome you are using if you don't know it beforehand):
# Chrome Driver 74 Download: https://chromedriver.storage.googleapis.com/index.html?path=74.0.3729.6/
# Chrome Driver 73 Download: https://chromedriver.storage.googleapis.com/index.html?path=73.0.3683.68/
New_Trial_URL = 'https://www.wordtopdf.com/'

driver.get(New_Trial_URL)
time.sleep(np.random.uniform(4.5, 5.5, size = 1)) # Time to load the page in peace

Find_upload = driver.find_element_by_xpath('//*[@id="file-uploader"]')

WORD_FILE_PATH = 'C:/Users/..../some_word_file.docx' # You need to specify this on your computer

Find_upload.send_keys(WORD_FILE_PATH) # Not working, no action happens here

Based on something very similar in JAVA (How to handle windows file upload using Selenium WebDriver?), this should work like a charm. But Voila... total failure and thus chance to learn something new.

I have also tried:

Click_Alert = Find_upload.click()
Click_Alert(driver).send_keys(WORD_FILE_PATH)

Did not work. 'Alert' should be inbuilt function as per these 2 links (https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium.webdriver.common.alert.html & Selenium-Python: interact with system modal dialogs).

But the 'Alert' function in the above link doesn't seem to exist in my Python setup even after executing

from selenium import webdriver

@All the readers, hope this doesn't take much of your time and we all get to learn something out of this.

Cheers

N V
  • 83
  • 2
  • 12

1 Answers1

0

You get ('//*[@id="file-uploader"]') which is <a> tag

but there is hidden <input type="file"> (behind <a>) which you have to use

import selenium.webdriver

your_file = "/home/you/file.doc"
your_email = "you@example.com"

url = 'https://www.wordtopdf.com/'

driver = selenium.webdriver.Firefox()
driver.get(url)

file_input = driver.find_element_by_xpath('//input[@type="file"]')
file_input.send_keys(your_file)

email_input = driver.find_element_by_xpath('//input[@name="email"]')
email_input.send_keys(your_email)

driver.find_element_by_id('convert_now').click()

Tested with Firefox 66 / Linux Mint 19.1 / Python 3.7 / Selenium 3.141.0


EDIT: The same method for uploading on zamzar.com

Situation which I saw first time (so it took me longer time to create solution): it has <input type="file"> hidden under button but it doesn't use it to upload file. It create dynamically second <input type="file"> which uses to upload file (or maybe even many files - I didn't test it).

import selenium.webdriver
from selenium.webdriver.support.ui import Select
import time


your_file = "/home/furas/Obrazy/37884728_1975437959135477_1313839270464585728_n.jpg"
#your_file = "/home/you/file.jpg"
output_format = 'png'

url = 'https://www.zamzar.com/'
driver = selenium.webdriver.Firefox()
driver.get(url)

#--- file --- 

# it has to wait because paga has to create second `input[@type="file"]`
file_input = driver.find_elements_by_xpath('//input[@type="file"]')
while len(file_input) < 2:
    print('len(file_input):', len(file_input)) 
    time.sleep(0.5)
    file_input = driver.find_elements_by_xpath('//input[@type="file"]')

file_input[1].send_keys(your_file)

#--- format ---

select_input = driver.find_element_by_id('convert-format')      
select = Select(select_input)
select.select_by_visible_text(output_format)

#--- convert ---

driver.find_element_by_id('convert-button').click()

#--- download ---

time.sleep(5)

driver.find_elements_by_xpath('//td[@class="status last"]/a')[0].click()
furas
  • 134,197
  • 12
  • 106
  • 148
  • Perfect. I wish I could upvote your answer a lot!!! I also found and luckily solved a minor bug: The upload code line doesn't accept '/' in file path and necessarily requires '\\'; general Python accepts both. Also Furas, based on your proficiency and experience and if not any problem, can you recommend any learning resource for advance Selenium? Language no problem for me, proficient in R/Python/JAVA/C/C# – N V Apr 09 '19 at 20:32
  • I learnt all with Google :) When I have problem then I search answer with Google. When Google gives me list of pages then there are some places which I check first - Stackoverflow.com and [Selenium with Python](https://selenium-python.readthedocs.io/index.html) I think I should read all pages on `Selenium with Python`. I also subcribed on [Elemental Selenium](http://elementalselenium.com/) but after 6 months I see they send the same tips. You can also see some tips in its [Archive](http://elementalselenium.com/tips) – furas Apr 09 '19 at 21:28
  • [Author of Elemental Selenium](http://davehaeffner.com/) wrote also book but it costs a lot. I will never buy it. – furas Apr 09 '19 at 21:31
  • I created code for [zamzar.com](https://zamzar.com) too. Portal creates second `` and uses it instead of first one. It took me longer time to see that there is second `` – furas Apr 09 '19 at 21:50