1

I need to automatize a file upload to a web page that is using some javascript code. To do so I'm using python selenium web driver to access the web page. Login the web page and browsing until the page where the file has to be uploaded works fine, but then I can't figure out how to click the choose file button.

This is how the code looks like until the point where I should upload the file:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import ui
from selenium.webdriver.support.wait import WebDriverWait    

def page_is_loaded(driver):
    return driver.find_element_by_tag_name("body") != None

base_url = 'https://www.xxxxxx.es/yyyyy/login.do?method=login'

driver = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver')
driver.get(base_url)

wait = ui.WebDriverWait(driver, 10)
wait.until(page_is_loaded)

username = driver.find_element_by_name('j_username')
username.send_keys('XXX')

password = driver.find_element_by_name('j_password')
password.send_keys('YYY')

accept = driver.find_element_by_name('aceptar')
accept.click()

# 'Carga de ficheros'
driver.find_element_by_xpath('//*[@id="cMI_1"]').click();
driver.find_element_by_xpath('//*[@id="cMI_1_1"]').click();

This is how the web page looks like: enter image description here

And this is how the page looks like when inspecting it: enter image description here

I've tried a lot of methods to 'click' the 'Choose File' button, with no success:

driver.find_element_by_xpath('//*[@id="tablaFormulario"]/tbody/tr[6]/td[2]/input').click
driver.find_element_by_css_selector('tablaFormulario').click()
driver.find_element_by_link_text('fichero').click()    
driver.find_element_by_link_text('Choose File').click() 
driver.find_element_by_class_name('contCampo').click()   
driver.find_elements_by_tag_name('fichero').click() 
driver.find_elements_by_tag_name('contCampo').click()

I suspect that it is related to the fact that the page runs with some javascript code... Any help will be appreciated!

1 Answer 1

2

Even if you able to click that button, you won't be able to handle file upload prompt with selenium. So you should not click() that button to upload file, but send path to file to it:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver.switch_to_frame('principal')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//input[@type="file"]'))).send_keys("C:\\path\\to\\file")
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help @Andersson! But The thing is that I receive a message saying that this element could not be located: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type="file"]"} (Session info: chrome=55.0.2883.87) (Driver info: chromedriver=2.27.440175 (9bc1d90b8bfa4dd181fbbf769a5eb5e575574320),platform=Linux 4.4.0-57-generic x86_64) This is the same problem I had with the other methods I tried, Selenium was unable to find the elements.
Check updated answer. Also check whether form not located inside an <iframe>
I didn't work, the table is inside an <iframe>: <iframe id="principal" name="principal" frameborder="0" style="position: absolute; overflow: auto; top: 106px; left: 0px; width: 100%; height: 884px; border: 0px;" src="prevision.do?method=obtenerListadoPrevisiones"> </iframe>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.