簡體   English   中英

如何使用Selenium Webdriver從Windows上傳文件

[英]How to upload file from windows using Selenium webdriver

我正在嘗試使用Selenium WebDriver上傳文件,

有一個彈出窗口帶有“瀏覽”按鈕,單擊“瀏覽”后,它將從Windows打開文件。

http://i.stack.imgur.com/wHYES.png

{
      //Switch focus on pop window
      String popupHandle = driver.getWindowHandle();
      WebDriver popup;
      popup = driver.switchTo().window(popupHandle);

      //Click on 'Browse Files' button.
      Thread.sleep(5000);
      WebElement filebrowse = driver.findElement(By.id("browse-files"));
      filebrowse.sendKeys("C:/Users/logo.jpg");
 }

我想上傳單個和多個文件,是否還有其他方法可以從窗口中選擇多個文件?

filebrowse.sendKeys(“ file:// C:/Users/logo.jpg”);

您需要提供絕對路徑。

我檢查了它的工作情況。

您的代碼看起來不錯。 我假設您的定位器能夠識別該按鈕,請先檢查以關閉彈出窗口,以確保Windows控制器已正確切換到彈出窗口

現在,問題是Windows和linux / mac被認為是斜杠/

如果您在Windows上工作,則使用forward \\ ;對於unix / mac,請使用back /

例:-

對於Windows,請使用以下語法

filebrowse.sendKeys("C:\Users\logo.jpg");

也可以嘗試

filebrowse.sendKeys("C:\\Users\\logo.jpg");

對於Mac / Linux,請使用以下語法

filebrowse.sendKeys("C:/Users/logo.jpg");

要么

filebrowse.sendKeys("C://Users//logo.jpg");

如果仍然無法正常工作,則最好發布您的HTML代碼,以便我們也可以檢查您的定位器

希望它能對您有所幫助:)

Selenium Webdriver不支持處理Windows對話,因為有第三方工具(如Sikuli和AutoIt)可用
為您(通過查看您的圖像)有兩種選擇

  1. 使用文本框上傳
  2. 使用Windows對話框(為此,您需要使用諸如AutoIt,Sikuli之類的工具)

對於文本框,首先需要檢查是否已啟用鍵入任何文本,然后傳遞文件的路徑。

Driver.findElement(by.id("")).sendKeys("path of file");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM