I'd like to capture the screenshot of the options that are displayed in the dropdown using selenium c# just like the image that is displayed below.
I've tried multiple ways to take the screenshot. Basically I've to expand the dropdown of the element to capture the screenshot. Here is what I've done
//#1
var element = Driver.FindElement(By.Id("carsId"));
Actions builder = new Actions(Driver);
builder.SendKeys(element, Keys.LeftAlt + Keys.Down).Build().Perform();
//#2
Actions act = new Actions(Driver);
act.MoveToElement(element).Build().Perform();
The first implementation to press Alt + Down keys worked manually when I've done on the site but didn't work through selenium. The second implementation didn't work either. I've also tried builder.ClickAndHold()
method as well.
And I've another question over here. Is it really possible for selenium to click and expand for a while until to grab the screen?
Any help would be greatly appreciated.
Link
Now, to capture the screenshot of desktop/application, we use Robot objects in Java.
For C#, you can use methods suggested in Capture screenshot of active window?.
Robot Sample Code:
This will take the screenshot of the entire screen and save it into the file on given file location.
Selenium can only take screenshot of options in custom dropdowns made using Javascript/CSS and not in select dropdown.
Let me know if above code works or you need more help.
To open the dropdown you just have to
.Click()
the element. So in your case,will expand the dropdown. The problem is that Selenium's screenshot functionality does not capture the open dropdown. You can get around that by using .NET to just take a screenshot of the active window. Working example code below.
Its really easy to do WITHOUT ANY ADDITIONAL TOOLS, believe me.
All you need is to do the following steps after click on Select element:
Create new Rectangle with position the same as have select and
var screenshotSize = lastOption.Position - Select.Position;
screenShotSize.height += lastOption.Size.Height;
Cut-off all except this Rectangle from the screenshot
In case of c# you can use for #6 the following code
And as result you will receive bitmap that contains exactly only expanded Select =)
so as you see, its really-really easy to do =) And you don't need any tools except selenium. And also browser window can be invisible at the moment (as exmample in case of using phantomJS)