2013-03-12 62 views
0

我需要使用Python和Selenium上传文件。当我点击上传的HTML元素时,将打开“文件上传”窗口,并且click()方法不会因为它等待完全加载页面而返回。因此我无法继续使用pywinauto代码来控制窗口。python webdriver os窗口

第一种方法点击HTML元素(一个img)上传新文件:

def add_file(self): 
    return self.selenium.find_element(By.ID, "add_file").click() 

和第二方法是使用pywinauto到路径输入到文件依次打开

def upload(self): 
    from pywinauto import application 
    app = application.Application() 
    app.connect_(title_re = "File Upload") 
    app.file_upload.TypeKeys("C:\\Path\\To\\FIle") 
    app.file_upload.Open.Click() 

如何强制add_file方法返回并能够运行上载方法?

回答

0

解决它。有一个iframe处理上传,但被隐藏起来,并没有看到它。 iframe包含也隐藏的类型文件的输入。为了解决这个问题作出的iframe可见使用javascript:

selenium.execute_script("document.getElementById('iframe_id').style.display = 'block';") 

然后切换到iframe和使输入也可见:

selenium.switch_to_frame(0) 
selenium.execute_script("document.getElementById('input_field_id').type = 'visible';") 

,只需发送路径输入:

selenium.find_element(By.ID, 'input_field_id').send_keys("path\\\\to\\\\file") 

对于Windows使用4'\\\'作为路径分隔符。