Selenium WebDriver文件上传浏览按钮C# [英] Selenium WebDriver file upload browse button c#

查看:247
本文介绍了Selenium WebDriver文件上传浏览按钮C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在浏览按钮和切换到文件对话框时遇到问题.我不能使用我的文件路径控件,而只能将包含文件路径和文件本身的字符串发送到那里,因为它是只读的,实际上,后面的控件是我的输入文件路径.

I have problem with browse button and switching to file dialog. I cannot use my file path control and just send there my string with file path and file itself, as it's readonly and in fact some behind control is my input filepath.

这是我的代码

driver.FindElement(By.Id("browseButton")).Click();
driver.SwitchTo().ActiveElement().SendKeys(filepath);

上面的代码填充了我对文件路径的控制,正如我在UI上看到的那样.但是我的打开文件对话框仍然打开,我不知道如何关闭它并提交我的上传文件.

Above code fills my control for file path, as i can see that on UI. But my open file dialog is still opened and i do not know how to close it and submit my upload.

推荐答案

许多方法之一是删除disable属性,然后使用典型的硒SendKeys()来实现这一目的

One of the many ways to do that is to remove the disable attribute and then use typical selenium SendKeys() to accomplish that

public void test(string path)
{
    By byId = By.Id("removeAttribute");
    const string removeAttribute = @"document.getElementById('browseButton').removeAttribute('disabled');";
    ((IJavaScriptExecutor)Driver).ExecuteScript(removeAttribute);
    driver.FindElement(byId).Clear();
    driver.FindElement(byId).SendKeys(path);
}

这篇关于Selenium WebDriver文件上传浏览按钮C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆