1

I am running the following code to get a logo screenshot but as it seems that location and size of element doesn't work when the element is in iframe. How element screenshot works in iframe ?

using System;
using System.Drawing;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

var _driver = new ChromeDriver();
_driver.Navigate().GoToUrl("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe");
_driver.SwitchTo().Frame("iframeResult");
_driver.SwitchTo().Frame(_driver.FindElement(OpenQA.Selenium.By.XPath("//iframe[@src='https://www.w3schools.com']")));
IWebElement element = _driver.FindElement(OpenQA.Selenium.By.XPath("//a[@class='w3schools-logo']"));
Console.WriteLine(element.Location); //Return 0
Console.WriteLine(element.Size); //Return 0
Screenshot sc = ((ITakesScreenshot)_driver).GetScreenshot();
using (var img = Image.FromStream(new MemoryStream(sc.AsByteArray)) as Bitmap)
{
 img?.Clone(new Rectangle(element.Location, element.Size), img.PixelFormat).Save(@"C:\test.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
}
0

1 Answer 1

1

I have found a solution

new Actions(_driver).MoveToElement(element).Build().Perform();
Thread.Sleep(1000);
var sc = ((ITakesScreenshot) _driver).GetScreenshot();
var remoteWebElement = element as RemoteWebElement;
using (var img = Image.FromStream(new MemoryStream(sc.AsByteArray)) as Bitmap)
{
if (remoteWebElement != null)
 img?.Clone(new Rectangle(remoteWebElement.LocationOnScreenOnceScrolledIntoView,remoteWebElement.Size), img.PixelFormat).Save(file, ImageFormat.Jpeg);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.