簡體   English   中英

Selenium WebDriver 和瀏覽器選擇文件對話框

[英]Selenium WebDriver and browsers select file dialog

我正在使用 selenium webdriver,C#。

是否可以使用 Firefox 選擇文件對話框制作工作 webdriver? 還是我必須使用 AutoIt 之類的東西?

如果您嘗試選擇要上傳的文件,Selenium 2 支持 HTML 文件輸入。 例如:

HTML

<input type="file" id="uploadhere" />

硒代碼

IWebElement element = driver.FindElement(By.Id("uploadhere"));
element.SendKeys("C:\\Some_Folder\\MyFile.txt");

基本上,您“鍵入”(使用SendKeys )文件輸入元素的完整文件路徑。 Selenium 為您處理文件選擇對話框。

但是,如果您想操作任意文件選擇對話框,那么就像 Anders 所說的那樣,您必須離開 Selenium。

不,WebDriver 不能與對話框交互 - 這是因為對話框是操作系統的域,而不是網頁。

我知道有人對 autoit 以及 .Net 提供的自動化 API 感到很幸運。

另一種選擇是完全跳過文件對話框並發出 POST 或 GET,但這需要更高級的網站知識以及了解如何構建 POST/GET。

您可以嘗試Webinator ,它類似於 Selenium,因為它由 WebDriver 提供支持。 它提供了文件對話框功能,我用它取得了巨大的成功。

這是使用 remotewebdriver 的另一種解決方案,它的作用就像魔術一樣,我喜歡它。

這是我的課程:

driver.findElementByLinkText("Upload Files").click();
driver.setLogLevel(Level.ALL);
System.out.println(driver.getCurrentUrl());
WebElement element = driver.findElement(By.xpath("//input[@name='file_1']"));
LocalFileDetector detector = new LocalFileDetector();

//Now, give the file path and see the magic :)              
String path = "D://test66T.txt";
File f = detector.getLocalFile(path);
((RemoteWebElement)element).setFileDetector(detector);
element.sendKeys(f.getAbsolutePath());

//now click the button to finish
driver.findElementByXPath("//html/body/div[9]/div[1]/a/span").click(); 

根據納迪姆·薩克 (Nadim Saker) 的說法

.Net 有一個庫來處理文件上傳對話框。 它有一個 SendKeys 類,該類有一個方法 SendWait(string keys)。 它在活動應用程序上發送給定的密鑰並等待消息被處理。 它不返回任何值。

這可以按如下方式完成,測試並使用 Internet Explorer 和 Chrome 驅動程序

var allowsDetection = this.Driver as IAllowsFileDetection;
if (allowsDetection != null)
{
   allowsDetection.FileDetector = new LocalFileDetector();
}

Driver.FindElement(By.Id("your-upload-input")).SendKeys(@"C:\PathToYourFile");

參考https://groups.google.com/forum/#!msg/webdriver/KxmRZ8MkM4M/45CT4ID_WjQJ

您要求在文件對話框中使用AutoIt 這很簡單,您可以使用C# 來完成

  • 安裝 nuget 包AutoItX.Net

  • 使用下面的演示代碼

  • 根據需要更改對話框標題字符串

     public static void InsertIntoFileDialog(string file, int timeout = 10) { int aiDialogHandle = AutoItX.WinWaitActive("Save As", "", timeout); // adjust string as you need if (aiDialogHandle <= 0) { Assert.Fail("Can't find file dialog."); } AutoItX.Send(file); Thread.Sleep(500); AutoItX.Send("{ENTER}"); Thread.Sleep(500); }

在我遇到與文件對話框相關的 Appium/Selenium 問題后,這對我有所幫助。

如果你想上傳文件,而不是使用 WebDriver,我遇到的唯一解決方案是AutoIt 它允許您編寫腳本並將其轉換為可執行文件,然后您可以在代碼中調用該腳本。 我在使用 ActiveX 控件時成功地使用了它。

另一種方法是使用System.Windows.Forms.SendKeys.SendWait("pathToFile")
我在任何地方都成功地使用它,我不能像@prestomanifesto 描述的那樣將密鑰發送到元素。

我用它來解決問題...如果以上都不起作用,請嘗試

    Actions action = new Actions(driver);
    action.SendKeys(pObjElement, Keys.Space).Build().Perform();

    Thread.Sleep(TimeSpan.FromSeconds(2));

    var dialogHWnd = FindWindow(null, "Elegir archivos para cargar"); // Here goes the title of the dialog window
    var setFocus = SetForegroundWindow(dialogHWnd);
    if (setFocus)
    {

        Thread.Sleep(TimeSpan.FromSeconds(2));
        System.Windows.Forms.SendKeys.SendWait(pFile);
        System.Windows.Forms.SendKeys.SendWait("{DOWN}");
        System.Windows.Forms.SendKeys.SendWait("{TAB}");
        System.Windows.Forms.SendKeys.SendWait("{TAB}");
        System.Windows.Forms.SendKeys.SendWait("{ENTER}");
    }

    Thread.Sleep(TimeSpan.FromSeconds(2));
}

暫無
暫無

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

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