Screenshot Fails in selenium-chromedriver If the URL contians query string parameter - java

I am using JDK 1.8.x. And I am capturing screenshots using selenium with the help of chromedriver.
Problem Facing:
If URL contains query String parameter. The screenshot looks like blank white empty page.
Example:
url = https://example.com?a=10&model=yellow
if " ?a=10&model=yellow " the query parameter present in the url. it returns the white empty page.
public void saveScreeshot()
{
DesiredCapabilities cap = DesiredCapabilities.chrome();
WebDriver driver = new ChromeDriver(cap);
driver.get("https://example.com?a=10&model=yellow");
JavascriptExecutor jse = (JavascriptExecutor) driver;
TakesScreenshot screen = (TakesScreenshot) driver;
File srcFile = screen.getScreenshotAs(OutputType.FILE);
File destFile = new File("myscreenshot.png");
}

Related

Unable to get the news articles link in google news page using Selenium

The below script goes to news page after searching for the text "Liverpool" and then prints all the links to a file and prints them in the console as well.
The problem here is I am unable to get the links to all the news articles in google news page. Except that all other links to the page are getting printed
public static void main(String[] args) throws IOException {
String URL = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(URL);
WebElement searchBar = driver.findElement(By.xpath("//*[#id='lst-ib']"));
searchBar.sendKeys("Liverpool");
WebElement clickSearch = driver.findElement(By.name("btnK"));
clickSearch.click();
WebElement newsButton = driver.findElement(By.xpath("//*[#id='hdtb-msb-vis']/div[2]/a"));
newsButton.click();
java.util.List<WebElement> links = driver.findElements(By.tagName("a"));
System.out.println(links.size());
FileWriter file = new FileWriter("/Users/lekharaj/Desktop/LFC.txt");
BufferedWriter b = new BufferedWriter(file);
for(int i=0;i<links.size();i++){
String text = links.get(i).getAttribute("href");
System.out.println("\n"+text);
b.write(text);
b.newLine();
b.flush();
}
}
To search for the text Liverpool on Google Home Page, and then print all the links you can use the following solution :
Code Block :
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.google.com/");
WebElement submit_button = driver.findElement(By.name("q"));
submit_button.sendKeys("Liverpool");
submit_button.submit();
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("News"))).click();
List <WebElement> my_list = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("h3.r.dO0Ag>a")));
System.out.println("The list of href links are : ");
for(WebElement element:my_list)
System.out.println(element.getAttribute("href"));
Console Output :
The list of href links are :
http://www.espn.com/soccer/club/liverpool/364/blog/post/3506156/jurgen-klopp-stability-at-liverpool-the-envy-of-their-premier-league-rivals
https://www.liverpoolfc.com/news/first-team/302415-liverpool-fc-songs-fans-europe
http://www.espn.com/soccer/club/liverpool/364/blog/post/3489163/liverpools-andrew-robertson-hoping-to-mimic-predecessor-alan-kennedys-heroics-against-real-madrid
https://www.liverpoolecho.co.uk/sport/football/transfer-news/christian-pulisic-refuses-comment-reports-14688647
https://www.liverpoolecho.co.uk/sport/football/football-news/liverpool-legend-terry-mcdermotts-three-14688746
http://www.skysports.com/football/news/11669/11381416/liverpool-transfer-rumours-gianluigi-donnarumma-daniel-ceballos-jamaal-lascelles-and-james-tarkowski
http://www.skysports.com/more-sports/ufc/news/29876/11380754/ufc-how-liverpool-built-darren-till
https://www.independent.co.uk/sport/football/transfers/liverpool-transfer-news-jurgen-klopp-shortlist-tarkowski-lascelles-premier-league-epl-a8361576.html
https://www.belfasttelegraph.co.uk/sport/football/premier-league/liverpool/this-real-madrid-star-can-crush-liverpools-champions-league-dream-says-giggs-36932660.html
http://kwese.espn.com/football/blog/transfer-talk/79/post/3506910/transfer-rater-neymar-to-real-madridjamaal-lascelles-to-liverpool
You can use the following x-path to get only article links.
//article/a
In code,
public static void main(String[] args) throws IOException {
String URL = "https://www.google.com";
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(URL);
WebElement searchBar = driver.findElement(By.xpath("//*[#id='lst-ib']"));
searchBar.sendKeys("Liverpool");
WebElement clickSearch = driver.findElement(By.name("btnK"));
clickSearch.click();
WebElement newsButton = driver.findElement(By.xpath("//*[#id='hdtb-msb-vis']/div[2]/a"));
newsButton.click();
java.util.List<WebElement> links = driver.findElements(By.xpath("//article/a"));
System.out.println(links.size());
FileWriter file = new FileWriter("/Users/lekharaj/Desktop/LFC.txt");
BufferedWriter b = new BufferedWriter(file);
for(int i=0;i<links.size();i++){
String text = links.get(i).getAttribute("href");
System.out.println("\n"+text);
b.write(text);
b.newLine();
b.flush();
}
}

ChromeDriver with headless raises ElementNotVisibleException

I am new in selenium. I need a browser without a graphical interface because the project will start with Jenkins. I decided to use ChromeDriver in Headdless mode.
When I use ChrimeDriver in normal mode, I can click on all elements:
WebDriver driver = new ChromeDriver();
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click(); // is ok
}
But when I use Headdless mode then I have: ElementNotVisibleException: element not visible. What could be wrong? Thank you for every clue.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click();//ElementNotVisibleException dont see next li elements
//div[#id='accordian']/div/ul/li
}
While working with Selenium Client v3.11.0, Chrome Driver v2.36 and Chrome Browser v65.x in Headless Mode, you need to pass the following arguments through an instance of ChromeOptions Class while initializing the WebDriver and the Web Browser as follows :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in");
You need to pass "--headless", chrome option like below.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
For entire list of chrome options, refer the following URL. It explains every command line switches in detail.
https://peter.sh/experiments/chromium-command-line-switches/
While working with headless mode, I encountered org.openqa.selenium.UnhandledAlertException due to not handling popping out of Alert Boxes. So it is better if you could handle the alert boxes.
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();

Selenium HtmlUnitDriver Angular2

I try to write a selenium test With HtmlUnitDriver it is:
HtmlUnitDriver driver = new HtmlUnitDriver();
//driver.setJavascriptEnabled(true);
driver.get(localhostURLAPI);
System.out.println("source of the page now is " + driver.getPageSource());
but in the result I have juste the HTML of index.html with an empty
any help is welcome
You need to change the line:
From :
HtmlUnitDriver driver = new HtmlUnitDriver();
To :
WebDriver driver = new HtmlUnitDriver();

Run TOR browser with Selenium WebDriver

Im currently trying to execute TOR 6.0.4 with Selenium WebDriver (JAVA) 2.53 and Firefox 43.0. I've followed the instructions from this post Using Selenium WebDriver with Tor but Im getting an error while loading the profilePath of TOR to the Firefox Binary. I've seen that is possible to lunch TOR by loading the TOR profile.default archive to the firefox binaty, but Im getting a Driver info: driver.version: unknown, when instantiating the binary with the profile. I've tried to change the firefox version and still. Below the code where I start the driver. Im also using Windows.
String torPath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Start Tor Browser.exe";
String profilePath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/webhp?complete=1&hl=en");
See below the exception thrown:
Exception in thread "main" org.openqa.selenium.WebDriverException: Specified firefox binary location does not exist or is not a real file: C:\Users\Jose Bernhardt\Desktop\Tor Browser\Start Tor Browser.exe
Seems that I was loading the Tor.exe and instead I had to load the firefox.exe from the Tor archive. I change my path to this and is working. Also fix that I was not sending the profile and the binary to the driver constructor
"C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\firefox.exe"
FirefoxDriver driver = new FirefoxDriver(binary, torProfile);
System.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");
String torPath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS,options);
WebDriver driver = new FirefoxDriver(options);
Till now Tor browser is supported using the Mozilla Firefox
Here is the code below:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\eclipse-workspace\\TorSelenium\\src\\main\\resources\\geckodriver.exe");//sa used for the firefox
String torBinaryPath = "C:\\Users\\user\\OneDrive\\Desktop\\Lk's stuff\\Tor Browser\\Browser\\firefox.exe"; //It is inside the tor browser's folder
Runtime runTime = Runtime.getRuntime();
Process torProcess = runTime.exec(torBinaryPath + " -n");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
WebDriver driver;
driver = new FirefoxDriver(firefoxOptions);
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebDriverWait wait;
wait = new WebDriverWait(driver, 30);
JavascriptExecutor js = (JavascriptExecutor) driver;
//added end
System.out.println(ean);
//Thread.sleep(100);
//driver.navigate().to("https://www.google.com/?hl=en");
//driver.navigate().to("https://duckduckgo.com/?q=d&ia=web");
driver.navigate().to("https://www.swiggy.com");
If you want to change the tor identity then you have to restart the tor using and again start above code:
Runtime.getRuntime().exec("taskkill /f /IM firefox");
Runtime.getRuntime().exec("taskkill /f /IM firefox.exe");
if(torProcess.isAlive()) {
System.out.println("destroying tor");
torProcess.destroy();
}
if(torProcess.isAlive()) {
System.out.println("forcebly destroying tor");
torProcess.destroyForcibly();
}

How to capture a remote website using Java Selenium

can anyone tell me how I can capture a webpage using Java Selenium? With an example....
See here: Capturing screenshots from remote Selenium RC.
In essence:
"To solve this you can use the captureScreenshotToString and captureEntirePageScreenshotToString commands, which return a Base64 encoded String of the screenshot, which you can then decode and save to disk on your testrunner machine."
I think this is what you are looking for. But try to be mores specific if it is not.
captureEntirePageScreenshot (
filename,kwargs )
Saves the entire contents of the current window canvas to a PNG file.
Contrast this with the
captureScreenshot command, which
captures the contents of the OS
viewport (i.e. whatever is currently
being displayed on the monitor), and
is implemented in the RC only.
Currently this only works in Firefox
when running in chrome mode, and in IE
non-HTA using the EXPERIMENTAL
"Snapsie" utility. The Firefox
implementation is mostly borrowed from
the Screengrab! Firefox extension.
Please see http://www.screengrab.org
and http://snapsie.sourceforge.net/
for details.
Arguments:
* filename - the path to the file to persist the screenshot as. No
filename extension will be appended by
default. Directories will not be
created if they do not exist, and an
exception will be thrown, possibly by
native code.
* kwargs - a kwargs string that modifies the way the screenshot
is captured. Example:
"background=#CCFFDD" . Currently valid
options:
background
the background CSS for the HTML document. This may be useful
to set for capturing screenshots of
less-than-ideal layouts, for example
where absolute positioning causes the
calculation of the canvas dimension to
fail and a black background is exposed
(possibly obscuring black text).
public static void getSnapShot(WebDriver driver, String event) {
{
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage originalImage = ImageIO.read(scrFile);
//int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
jpeg.setAlignment(Image.MIDDLE);
++index;
} catch (Exception e) {
e.printStackTrace();
}
}
}
I like to use PhantomJS driver for taking screenshots.
public class Test {
public static void main(String[] args) {
//PhantomJS headless driver
File file = new File("D:\\Webdriver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
//Set Size here
driver.manage().window().setSize(new Dimension(1600,900));
//To wait until the element get visible or invisible.
WebDriverWait wait = new WebDriverWait(driver, 25);
//To access url.
driver.get("https://www.google.co.in");
//For wait until the element get visible.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));
File shot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(shot, new File("D:\\Webdriver\\Capture.jpg"));
}
}
It takes full page screenshot of chrome webpage.
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
String baseUrl = "https://www.google.co.in";
driver.get(baseUrl);
String fullscreen =Keys.chord(Keys.F11);
driver.findElement(By.cssSelector("body")).sendKeys(fullscreen);
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("F://test.png");
FileUtils.copyFile(SrcFile, DestFile);
driver.close();
I see a lot of answers explaining screenshots, but just incase you are asking how to get the entire source of the page use the following method:
String pageSource = driver.getPageSource();
Here is a runnable example.

Categories