Python 2.7 在Javascript Web中使用Python Selenium上载文件

Python 2.7 在Javascript Web中使用Python Selenium上载文件,python-2.7,selenium-webdriver,Python 2.7,Selenium Webdriver,我需要将文件自动上传到使用javascript代码的网页。为此,我使用python selenium web驱动程序来访问网页。登录网页并浏览,直到上传文件的页面工作正常,但我不知道如何单击“选择文件”按钮 在我应该上传文件之前,代码是这样的: from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import ui from s

我需要将文件自动上传到使用javascript代码的网页。为此,我使用python selenium web驱动程序来访问网页。登录网页并浏览,直到上传文件的页面工作正常,但我不知道如何单击“选择文件”按钮

在我应该上传文件之前,代码是这样的:

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();
这是网页的外观:

这是检查页面时页面的外观:

我尝试了很多方法来“单击”选择文件按钮,但没有成功:

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()

我怀疑这与页面运行某些javascript代码有关。。。任何帮助都将不胜感激

即使您能够单击该按钮,也无法使用
selenium
处理文件上载提示。因此,您不应该
单击()
该按钮上载文件,而是将文件路径发送到该按钮:

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")

谢谢你的帮助@Anderson!但问题是,我收到一条消息,说这个元素无法定位:
NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:”//input[@type=“file”]“}(会话信息:chrome=55.0.2883.87)(驱动程序信息:chromedriver=2.27.440175(9bc1d90b8bba4dd181fbbf769a5eb5e57574320),platform=Linux 4.4.0-57-generic x86_64)
这与我尝试的其他方法的问题相同,Selenium无法找到元素。请检查更新的答案。还要检查表单是否不在
中我没有工作,表格是否在
中: