问题
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.
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.");
回答1:
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.
Then just use:
driver.findElement(by.xpath('input xpath or id ..').sendKeys('path of the image in your system');
来源:https://stackoverflow.com/questions/53940603/selenium-c-sharp-test-with-chrome-headless-chrome-file-upload-window-not-worki