代码之家  ›  专栏  ›  技术社区  ›  Reflux

Webdriver:文件上载

  •  12
  • Reflux  · 技术社区  · 14 年前

    有没有一种方法可以与webdriver中的文件上传框进行交互?路径所在的表单字段是只读的,因此我无法写入该字段。

    6 回复  |  直到 14 年前
        1
  •  11
  •   Jeremy Paskali Sergii Pozharov    8 年前

    您可以使用JavaScript设置输入字段的值。考虑到字段的id是 fileName 下面的示例将设置文件的输入值 C:\temp\file.txt :

    String script = "document.getElementById('fileName').value='" + "C:\\\\temp\\\\file.txt" + "';";
    ((IJavaScriptExecutor)driver).ExecuteScript(script);
    

    在这个例子中, driver 是您的WebDriver实例。

    \ )对于类似Windows的路径,因为需要向JavaScript传递双反斜杠,所以必须使用两个附加斜杠来转义这两个斜杠。另一个选择是使用正斜杠(例如。 "C:/tmp/file.txt"

        2
  •  21
  •   Ben Butler-Cole    14 年前

    不用注入JavaScript就可以做到这一点。您只需要获得表单字段并将其输入。类似于(使用Ruby API):

    driver.find_element(:id, 'upload').send_keys('/foo/bar')
    
        3
  •  7
  •   Gudradain    9 年前

    对于C#, SendKeys() \ 在文件路径中而不是 /

    例如,以下工作:

    string filePath = @"drive:\path\filename.filextension";
    driver.FindElement(By.Id("fileInput")).SendKeys(filePath);
    

    但以下方法不起作用:

    string filePath = "drive:/path/filename.filextension";
    driver.FindElement(By.Id("fileInput")).SendKeys(filePath);
    
        4
  •  3
  •   JamesThomasMoon    9 年前

    元素。单击 在上载对话框关闭之前不会返回调用上载对话框的。说清楚点, 表示操作系统本机文件选择。

    这是我的解决方案(有点复杂,但是* *大多数解决SeleniumWebDriver问题的方法必须变得复杂)。

    # presumes webdriver has loaded the web page of interest
    element_input = webdriver.find_element_by_css_selector('input[id="uploadfile"]')
    handle_dialog(element_input, "foobar.txt")
    
    def handle_dialog(element_initiating_dialog, dialog_text_input):
        def _handle_dialog(_element_initiating_dialog):
            _element_initiating_dialog.click() # thread hangs here until upload dialog closes
        t = threading.Thread(target=_handle_dialog, args=[element_initiating_dialog] )
        t.start()
        time.sleep(1) # poor thread synchronization, but good enough
    
        upload_dialog = webdriver.switch_to_active_element()
        upload_dialog.send_keys(dialog_text_input)
        upload_dialog.send_keys(selenium.webdriver.common.keys.Keys.ENTER) # the ENTER key closes the upload dialog, other thread exits
    


        5
  •  1
  •   Ashish Kamble    6 年前

    我也在寻找第三方图书馆,

    除非没有其他窗口,否则这对我很有用:

    在C#中添加System.Windows.Forms的引用

    using System.Windows.Forms;
    
    string url = "http://nervgh.github.io/pages/angular-file-upload/examples/image-preview/";
    string path = @"C:\Users\File_Path";
    IWebDriver d = new ChromeDriver();
    d.Navigate().GoToUrl(url);
    d.FindElement(By.XPath("//input[@type='file']")).Click();
    hread.Sleep(5000);
    System.Windows.Forms.SendKeys.SendWait(path);
    System.Windows.Forms.SendKeys.SendWait(@"{Enter}");
    
        6
  •  0
  •   izik f    4 年前

    在我的情况下,我可以上传类似文件的解决方案,写入hear,但对话框窗口卡住了进程,驱动程序无法引用关闭此窗口,因此我手动杀死了他:

     foreach (var p in Process.GetProcessesByName("chrome"))
            if (p.MainWindowTitle.ToLower().Contains("open"))
                   p.Kill();
    
        7
  •  -1
  •   Thiyagarajan Veluchamy    12 年前

    @driver.find_element(:xpath, "html/body/div[1]/div[2]/div[1]/form/div[4]/div[7]/table/tbody/tr[1]/td[2]/input").send_keys "C:\\Users\\Public\\Pictures\\Sample Pictures\\Chrysanthemum.jpg"
    

    这是帮我上传的图片。