2015-02-11 259 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

相关问题