带有Chrome Headless的Selenium C#测试-Chrome文件上传窗口不起作用 [英] Selenium C# Test with Chrome Headless - Chrome File Upload Window not working

查看:286
本文介绍了带有Chrome Headless的Selenium C#测试-Chrome文件上传窗口不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试,可以将个人资料图片上传到Web应用程序.为了上传图像文件,请单击一个上传按钮,这将打开一个文件上传窗口,如下面的屏幕截图所示.

I have a test which uploads a profile image to the web application. In order to upload the image file, an upload button is clicked which opens a file upload window as shown in the screenshot below.

然后,测试会将图像文件路径的键发送到文件上传窗口,以上传文件.在chrome无头模式下运行测试时,找不到文件上传窗口,并且发送键步骤失败.

The test then sends keys of the image file path to the file upload window in order to upload the file. When the test is run in chrome headless mode, the file upload window is not found and the send keys step fails.

如果测试在正常模式下运行(没有无头选项),则测试运行正常,没有任何问题.可能是什么问题呢?有没有什么办法解决这一问题?以下代码用于设置焦点并将密钥发送到浏览器文件上传窗口.

If the test is run in normal mode (without headless option) the test runs fine without any issue. What could be the problem? Is there any way to fix this? Following code is used to set focus and send keys to the browser file upload window.

 var dialogHWnd = FindWindow("#32770", "Open"); // Title for modal. IE: "Choose File to Upload"
 var setFocus = SetForegroundWindow(dialogHWnd);
 SendKeys.SendWait(@picPath);
 Thread.Sleep(5000); //wait
 SendKeys.SendWait(@"{Enter}");
 Reporter.LogTestStepForBugLogger(Status.Info, $"Sent keys {picPath} and Enter to upload the Profile Picture.");

推荐答案

使用硒,您可以将图像的完整路径发送到负责上传的输入元素.

With selenium, you can just sent the full path of the image to the input element that is responsible for the upload.

检查html代码并搜索可能带有隐藏"属性的相关内容或此上载部分的常用内容.

Check the html code and search for relevant probably with "hidden" attribute or something common for this upload section.

然后只使用:

driver.findElement(by.xpath('input xpath or id ..').sendKeys('path of the image in your system');

这篇关于带有Chrome Headless的Selenium C#测试-Chrome文件上传窗口不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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