If driver switched to , would take screenshot of content only

I tried to reproduce your issue and looks like you are not completely right. In my case when I took the screen-shot it contained the entire page despite I switched to iFrame before.

if it’s possible to get location of an relative to iframe and not the whole screen?

getLocation method returns you the position relative to iFrame. I.e. exactly what you need. So in order to get the position of an element in the entire view you should take the position of iframe in the parent view and add the position of the elemetn in iframe. Here is the proof-of-concept:

Test site: https://jsfiddle.net/o1kqLahu/5/

Code:

public static void main(String[] args) throws IOException {

    System.setProperty(".chrome.driver", "C:\Temp\chromedriver.exe");
     chromeDriver = new ChromeDriver();
    chromeDriver.get("https://jsfiddle.net/o1kqLahu/5/");
    Point locationOfIFrame = chromeDriver.findElement(By.name("result")).getLocation();
    System.out.println("X: " + locationOfIFrame.x + "; Y: " + locationOfIFrame.y);
    chromeDriver.switchTo().frame("result");
    Point locationInIFrame = chromeDriver.findElement(By.id("secret-input")).getLocation();
    File scrFile = ((TakesScreenshot)chromeDriver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("c:\Temp\screenshot2.png"));
    System.out.println("Location of element in iframe: X: "
            + locationInIFrame.x + "; Y: " + locationInIFrame.y);
    System.out.println("Location of element of the screen: X: " +
            (locationInIFrame.x + locationOfIFrame.x) +
            "; Y: " +
            (locationInIFrame.y + locationOfIFrame.y));
    chromeDriver.close();

}

Output:

X: 570; Y: 474
Location of element in iframe: X: 8; Y: 8
Location of element of the screen: X: 578; Y: 482

Some screens saved:

enter image description here

enter image description here

UPD:

In case of the defect in geko-driver (as per the update of your question) I would recommend the following workaround:

  1. Take iFrame position before switching to that iFrame
  2. Take position of the required element (related to the entire screen)
  3. Take screenshot.

Now we have the picture of the iFrame content and we can calculate the position of the element inside the iFrame using the following formula:

PositionOfElementInsideIFrame = PositionOfTheElementInsideEntireScreen - PositionOfIFrameInsideEntireScreen



Source link https://sqa.stackexchange.com/questions/32617/how-to-get--of-element-inside-iframe

LEAVE A REPLY

Please enter your comment!
Please enter your name here