简体   繁体   English

带有Chrome Headless的Selenium C#测试-Chrome文件上传窗口不起作用

[英]Selenium C# Test with Chrome Headless - Chrome File Upload Window not working

I have a test which uploads a profile image to the web application. 我有一个将个人资料图片上传到Web应用程序的测试。 In order to upload the image file, an upload button is clicked which opens a file upload window as shown in the screenshot below. 为了上传图像文件,单击一个上传按钮,打开一个文件上传窗口,如下面的屏幕快照所示。

浏览器文件上传窗口

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. 在chrome无头模式下运行测试时,找不到文件上传窗口,并且发送键步骤失败。

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. 使用硒,您可以将图像的完整路径发送到负责上传的输入元素。

Check the html code and search for relevant probably with "hidden" attribute or something common for this upload section. 检查html代码,并搜索可能带有“隐藏”属性的相关内容,或此上载部分的常用内容。

Then just use: 然后使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM