使用Selenium在Electron应用中处理Windows文件夹选择对话框问题
自动化Electron应用时的系统文件夹选择框问题
我目前使用C#结合Selenium自动化Electron应用,多数操作与浏览器自动化类似,但遇到一个问题:应用中有一个按钮,点击后会打开Windows文件夹选择对话框,选择文件夹后路径会显示在按钮旁的输入框中,手动操作只能通过该系统对话框完成。
对应的HTML代码片段:
<div class="FormControl-root"> <div class="Box-root"> <div class="Box-root"> <button class="ButtonBase-root" tabindex="0" type="button" style="white-space: nowrap;">Choose Folder <span class="Touch-root"></span></button> </div> <div class="Box-root"> <div class="FormControl-root "> <label class="FormLabel-root" data-shrink="true" for=":r10:" id="r-label">Folder path</label> <div class="InputBase-root" style="pointer-events: none;"> <input aria-invalid="false" id="r-input" type="text" class="InputBase" value="Select Folder"> <fieldset aria-hidden="true" class="OutlinedInput"> <legend class="css-1"><span>Folder path</span></legend> </fieldset> </div> </div> </div> </div> </div>
已尝试的无效方法
- 对按钮元素使用SendKeys传入文件夹路径:仅打开系统对话框,无其他效果。
public void SupplyFolderPath(string path) { var selectFolderButton = _driver.FindElement(By.XPath("//button[@class='ButtonBase-root']")); selectFolderButton.SendKeys(path); } - 对输入框元素使用SendKeys传入路径:仅在输入框中出现光标,未填入内容。
public void SupplyFolderPath(string path) { var folderField = _driver.FindElement(By.Id("r-input")); folderField.SendKeys(path); } - 使用Javascript Executor设置输入框的value属性:完全无效。
public void SupplyFolderPath(string path) { var folderField = _driver.FindElement(By.Id("r-input")); var je = (IJavaScriptExecutor)_driver; je.ExecuteScript($"arguments[0].value='{path}';", folderField); } - 使用Javascript Executor将按钮的type属性改为file后再用SendKeys:仍仅打开系统对话框,无效果。
public void SupplyFolderPath(string path) { var selectFolderButton = _driver.FindElement(By.XPath("//button[@class='ButtonBase-root']")); var je = (IJavaScriptExecutor)_driver; je.ExecuteScript($"arguments[0].setAttribute('type', 'file')", selectFolderButton); }
现在已无其他思路,希望能得到技术建议,是否必须更换工具?
内容的提问来源于stack exchange,提问作者user1832478
相关产品推荐
相关产品推荐

