2015-02-11 65 views
1

我試圖使用Selenium WebDriver上傳文件。問題是硒沒有發送文件路徑到對話窗口。我正在使用SendKeys()。這裏是我的代碼:使用Selenium WebDriver上傳文件 - 無法與對話框窗口交互C#

Click.DsrSubmitNewActivityToolPage.AttachmentButton(); 
Thread.Sleep(4000); 
Actions action = new Actions(PageElements.Driver); 
action.SendKeys("C:/Users/gk/Documents/Test/Test Test.docx"); 
action.SendKeys(Keys.Enter); 
Thread.Sleep(4000); 

這裏是HTML:

<div class="activity yui3-g"> 
    <div class="label yui3-u-1-5"> 
     <span>Attachment:</span> 
    </div> 
    <div class="yui3-u-4-5"> 
     <input id="fileID" type="file" name="file"/> 
    </div> 
</div> 
<div id="pointValueContainer" class="activity yui3-g" style="display:none"> 
<span class="label">Notes</span> 
<div class="activity"> 
<p> 

+0

你能提供'html'嗎? – Saifur 2015-02-11 17:50:24

+0

@Gala_De你缺少'.Perform()'方法調用來完成操作。 – 2015-02-11 18:05:38

+0

感謝您的回覆@Vivek Singh。爲了您的方便,我附上了html。 – 2015-02-13 22:05:20

回答

0

看起來你是在驅動器/根,而不是目標元素進行SendKeys()

您需要找到目標文件標籤並在其上使用SendKeys()。 EG:

public void Test(string filepath) 
{ 
    By byCss = By.CssSelector("your selector"); 
    //explicit wait to make sure the element is visible 
    new WebDriverWait(Driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(byCss)); 
    Driver.FindElement(byCss).SendKeys(filepath); 
} 
0

如果你正在嘗試與本地文件對話框交互,那麼硒能不能做到這一點。

你可以使用像AutoIT這樣的東西來自動化對話框,但我會強烈建議它;您稍後會遇到跨瀏覽器和可擴展性問題。

相反,您可以在與文件上傳關聯的位置執行send_keys。在html中查看,在文件上傳控件中肯定會有一個。

只需使用send_keys插入文件的路徑就會導致上載。 您不必使用ActionBiilders

+0

謝謝@Vivek Singh – 2015-02-16 22:13:39

0

所以自類型的文件 u能直接執行driver.FindElement(By.Id("fileID")).SendKeys(filepath);

但在使用操作類記得你需要調用Build(如果你要多行動,以便在爲了合併它們),然後是Perform(執行提供的操作)。

雖然我是一個java的傢伙但我已經看到的SendKeys方法,如果您所提供的文件路徑有空間,就無法得到的文件,所以我會建議創建一個文件對象,並在sendkeys發送文件的absolutePath

相關問題