java - How to open target ='_blank' Web Element and get it's screenshot? -


the testpage working on http://www.quackit.com/html/templates/frames/frames_example_1.html in firefox.

in have click on code generator webelement , take screenshot of opened page selenium webdriver, java. whatever method tried, selenium giving exception as

unable locate element.

it has target _blank.

please find code below

import java.io.file; import java.io.ioexception; import java.util.concurrent.timeunit;  import org.apache.commons.io.fileutils; import org.openqa.selenium.by; import org.openqa.selenium.javascriptexecutor; import org.openqa.selenium.keys; import org.openqa.selenium.outputtype; import org.openqa.selenium.point; import org.openqa.selenium.takesscreenshot; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.firefox.firefoxdriver; import org.openqa.selenium.interactions.actions; import org.openqa.selenium.support.ui.expectedconditions; import org.openqa.selenium.support.ui.webdriverwait;  public class quackit {     public static void main(string[] args) throws exception {         webdriver driver = new firefoxdriver();         driver.get("http://www.quackit.com/html/templates/frames/frames_example_1.html");         driver.manage().window().maximize();         driver.manage().timeouts().implicitlywait(10,timeunit.seconds);         string parentwindow = driver.getwindowhandle();         webelement e = driver.findelement(by.linktext("code generator "));      // ensure link opens in current window         javascriptexecutor js = (javascriptexecutor) driver;         js.executescript("arguments[0].setattribute('target', arguments[1]);", e, "_self");           e.click();         (string winhandle : driver.getwindowhandles()) {             driver.switchto().window(winhandle); // switch focus of webdriver next found window handle (that's newly opened window)         }          takesscreenshot ts = (takesscreenshot)driver;         file source = ts.getscreenshotas(outputtype.file);         fileutils.copyfile(source,new file ("./screenshots/codegenerator.png"));      } } 

this works:

        driver.switchto().frame("content");         webelement e = driver.findelement(by.xpath("//a[contains(.,'code generator')]"));         e.click(); 

you have switch frame first , should use contains() locate element.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -