Java Selenium, storing updated page source after javascript activation - javascript

I have managed to open a browser with my link and activate the javascript, which allows the page to display more results. Once this is done I am trying to print the new updated page source in the console but all it shows is the original source prior to the javascript activation. My code so far is shown below.
WebDriver driver = new FirefoxDriver();
driver.get("www.desiredLink.com");
if (driver instanceof JavascriptExecutor)
{
((JavascriptExecutor)driver).executeScript("javascriptFunction();");
System.out.println(driver.getPageSource());
}
else
{
throw new IllegalStateException("No support for JavaScript!");
}

You have to get attribute "innerHTML" of body instead of pageSource:
String bodyHtml = driver.findElement(By.tagName("body")).getAttribute("innerHTML");

Related

Selenium with headless chrome

I have testing environment which is perfectly working with chrome driver in desktop mode. I am using some javascript injections (everything works) f.e.:
public static void ForceFillInput(this Driver driver, string selector, string value)
{
var javaScriptExecutor = (IJavaScriptExecutor)driver.webDriver;
javaScriptExecutor.ExecuteScript($"$(\"{selector}\").val(\"{value}\")");
}
but when i want to run it in headless mode
AddArguments("--headless")
it will just fail on
"$ is not defined"
Can somebody help me how to inject js/jquery into headless solution?
M.
your Javascript snippet used jQuery api. In modern web development, we put Javascript at the end of HTML page to let browser to load javascript at last, so that static resources (like picture/image/text content) can display earlier as possible, withing this way to improve user experience when user open website.
I think your page also put jQuery at the end to load, try add some wait/sleep before ExecuteScript to wait browser complete load jQuery.
It looks like the shorthand for JQuery is not yet created at the time your script is executed.
Use a waiter to wait for JQuery and for the selector to be found:
public static void ForceFillInput(this Driver driver, string selector, string value)
{
string JS_SET_VALUE =
"var e; return !!window.$ && (e = window.$(arguments[0])).length > 0 && (e.val(arguments[1]), true);";
new WebDriverWait(driver, TimeSpan.FromSeconds(60))
.until(ctx => (bool)((IJavaScriptExecutor)ctx).ExecuteScript(JS_SET_VALUE, selector, value));
}

Opening a new tab using Ctrl + click combination in Selenium Webdriver

I am attempting to use ctrl + click on a link to open it in a new tab. This is working fine in Chrome 58. Please find the code below:
action.keyDown(Keys.CONTROL).click(driver.findElement(By.xpath
("//section[#class='filmStrip__basic']//a[text()='En savoir
plus']"))).keyUp(Keys.CONTROL).build().perform();
I am using the same code on IE, Firefox and Safari but getting the following error:
Firefox 54: The link is getting open in the same tab.
IE 11: Nothing happening.. the control is moving to the next line
Safari: exception on action.keyDown-Unrecognized command
Help related to any one browser is also appreciated.
Thanks
As you are trying to click on a link which is within a <a> tag, instead of xpath you can use the linkText locator. Here is the sample code with opens the url http://www.google.com, verifies the Page Title, uses Actions class to click on the Gmail link to open https://accounts.google.com in a new tab.
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
You can find a relevant Python based solution in How to open a link embed in web element in the main tab, in a new tab of the same window using Selenium Webdriver
Another way is to use javascript executor:
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.open('','_blank');");
As for your problem I had it too and didn't find anything usefull until I found this workaround.
I even tried: solution with ctrl + enter
try this way....
// specify chromedriver.exe directory path and replace in "driverPath"
String driverPath = "C:/Users......";
WebDriver driver;
System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe");
driver = new ChromeDriver();
System.out.println("lanuching 1st url in tab1");
driver.navigate().to(
"https://amazon.com");
System.out.println("lanuched 1st url in tab1");
Thread.sleep(30000);
((JavascriptExecutor) driver).executeScript(
"window.open('http://ebay.com');");
Thread.sleep(20000);
Set<String> allwh = driver.getWindowHandles();
System.out.println(allwh.size());
for (String v : allwh) {
System.out.println(v);
driver.switchTo().window(v);
String title = driver.getTitle();
System.out.println("2nd url in tab2" + title);

Selenium-Webdriver Node.js Can't Find New Window

I'm running a selenium-webdriver javascript scraper, which logs into a site and clicks a button that launches a new tab/window. I'm trying to switch the driver to focus on the newly generated window, but Selenium cannot find it. The code I have to look:
driver.sleep(10000).then(function() {
driver.getAllWindowHandles().then(function(d) {
console.log(d);
})
})
Which prints
[ '287ab61a-b155-46de-a2a6-298e0e98e440' ]
Which is the original browser window. What could cause Selenium to not pick up on the new window?
This is what I use whenever I need to switch to a new tab:
const tabs = await driver.getAllWindowHandles();
await driver.switchToWindow(tabs[1]);
If you want to switch back to the main tab just use tab[0].

Having trouble interacting with wep pages using Selenium in Java

I'm trying to use the HtmlUnitDriver and WebElement classes of Selenium in Java to click the "Download as CSV" button on Google trends.
The problem I'm having is that that button is hidden (not displayed) until you click a different settings menu button, but I can't click that settings menu button with WebElement.
Here is my code:
/**
* #args String, the term to search on Google Trends
*/
public static void main(String[] args)
{
//instantiate an HtmlUnitDriver
HtmlUnitDriver hud = new HtmlUnitDriver();
//navigate to the 90-day Google Trends page of the input term in args
hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");
//set element to the first button to press
WebElement element = hud.findElement(By.id("settings-menu-button"));
//click the element
element.click();
}
The error I am getting is: org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements
But the settings menu button is visible?
This is my first time making a program like this and using this library, so thanks for any help. I'm still learning.
Can you try this
public static void main(String[] args)
{
//instantiate an HtmlUnitDriver
HtmlUnitDriver hud = new HtmlUnitDriver();
wait = new WebDriverWait(hud , 120);
//navigate to the 90-day Google Trends page of the input term in args
hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("settings-menu-button")).click();
}
Switch to the real browser (e.g. Firefox, Chrome):
ChromeDriver hud = new ChromeDriver();
Reasons:
https://code.google.com/p/selenium/wiki/HtmlUnitDriver
None of the popular browsers uses the javascript engine used by
HtmlUnit (Rhino). If you test javascript using HtmlUnit the results
may differ significantly from those browsers.
https://gist.github.com/evandrix/3694955
Headless browsers that have JavaScript support via an emulated DOM
generally have issues with some sites that use more advanced/obscure
browser features, or have functionality that has visual dependencies
(e.g. via CSS positions and so forth)

programmatically print a web page

I am making a program to automatically go to a website and print a page, Can't seem to make it work, I tried selenium chrome driver, problem is it doesn't work. i tried action.sendkeys keys ctrl + shift + p to no avail, the biggest problem is print preview pop-up.
I tried sending JavaScript command: window.print(), but the print preview in chrome stands in my way, because you need to press enter. Is there a way in JavaScript to simulate the pressing of the ENTER key? Help would be appreciated.
Well, after a bit of research, I found this video, if you can add these switches: "--kiosk --kiosk-printing", to the chrome driver start, it would automatically skip the print preview prompt, just as shown in the video.
also, I tested this on the latest version of SRWare iron(fork of chromium), and it worked.
If you are using C# to make your program, then there is an easier solution:
private void Button1_Click(object sender, EventArgs e)
{
PrintHelpPage();
}
private void PrintHelpPage()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(#"http://www.google.com"); //This is what you want to change
}
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
Found the answer in Here, this uses the WebBrowser control to navigate to a specific Url, can be a local url or from the internet, and prints it using your default printer.
Maybe you could you use this approach that doesn't require windows forms, worked like a charm for me:
With C# use Chrome to covert HTML to PDF
var process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
var chrome = Path.Combine(Environment.GetEnvironmentVariable("ProgramFiles(x86)"), #"Google\Chrome\Application\chrome.exe");
// use powershell
process.StartInfo.FileName = "powershell";
// set the Chrome path as local variable in powershell and run
process.StartInfo.Arguments = "$chrome='" + chrome + #"'; & $chrome --headless --print-to-pdf='c:\Users\" + Environment.UserName + #"\desktop\myReport.pdf' https://google.com";
process.Start();

Categories