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)
Related
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));
}
I am currently developing an application in php / js / html and I would like to launch an application made in C # via a html button (no download) however I use Mozilla Firefox and the only solution I found is a script featuring ActiveXObject which is only used by IE apparently.
You can use the Firefox command line options of -url
"C:\Program Files\Mozilla Firefox\firefox.exe" -url "https:\\www.stackoverflow.com"
or
Use selenium Webdriver, this can launch the browser to any url you specify and can fill any form on the page with any details you specify and press any buttons that you can find.
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using SeleniumExtras.WaitHelpers;
class HelloSelenium
{
static void Main()
{
using (IWebDriver driver = new FirefoxDriver())
{
WebDriverWait wait = new WebDriverWait(driver);
driver.Navigate().GoToUrl("https://www.stackoverflow.com/ncr");
driver.FindElement(By.Name("q")).SendKeys("Vote Up" + Keys.Enter);
}
}
}
I did a simple demo when useing JxBrowser Calling Java from JavaScript, and then I found the phenomenon,
public static void main(String[] args) {
// TODO Auto-generated method stub
BrowserPreferences.setChromiumSwitches("--remote-debugging-port=9222");
Browser browser1 = new Browser();
BrowserView browserView1 = new BrowserView(browser1);
// Gets URL of the remote Developer Tools web page for browser1 instance.
String remoteDebuggingURL = browser1.getRemoteDebuggingURL();
browser1.addScriptContextListener(new ScriptContextAdapter() {
#Override
public void onScriptContextCreated(ScriptContextEvent event) {
Browser browser = event.getBrowser();
JSValue window = browser.executeJavaScriptAndReturnValue("window");
window.asObject().setProperty("java", new JavaObject());
}
});
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame1.add(browserView1, BorderLayout.CENTER);
frame1.setSize(700, 500);
frame1.setLocationRelativeTo(null);
frame1.setVisible(true);
browser1.loadURL("https://www.baidu.com");
// Creates another Browser instance and loads the remote Developer
// Tools URL to access HTML inspector.
Browser browser2 = new Browser();
BrowserView browserView2 = new BrowserView(browser2);
JFrame frame2 = new JFrame();
frame2.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame2.add(browserView2, BorderLayout.CENTER);
frame2.setSize(700, 500);
frame2.setLocationRelativeTo(null);
frame2.setVisible(true);
browser2.loadURL(remoteDebuggingURL);
}
I run the code and refresh the chrome, many times and then
before refresh
refresh 2 Minute
if user other network with more resources or images ,The Memory increase more faster, Would like to ask whether this problem can be solved
thanks all!
I'm looking forward to the reply!
I checked your example and I confirm that it is an issue in JxBrowser. I've created the corresponding task in our issue tracking system. We will fix this issue in one of the next JxBrowser versions. I will let you know when the new build with the fix is available for download.
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();
Background
I have a network performance testing tool which after every test,displays all its results in a new results page. Below is my workflow to find the final value.(Details not relevant here but the html elements i am looking for might
1.Go to a section called "Data frame" ( which has a table with timestamp,transmit,recieve and percentage as columns) and get the time stamp when percentage first drops below 99.9
Here is a snippet of the "Data Frame" table
Data frame snippet
2.Go to another section called "Data throughput" ( table with timestamp, throughput as columns) and get the maximum value of throughput before the previously found timestamp.
After every test, i have to do this manually, but there should be someway to automate this.
Question
1.Is it possible to write a script ( maybe in javascript ) to automate my workflow for every new results page?
example algorithm
section = webpage.section_with_title("Data frames")
for each row in section
find first my_timestamp with percent < 99.9
done
section2 = webpage.section_with_title("Data Throughput")
for each row in section2
find max row.throughput if row.timestamp < my_timestamp
don
2.Once i write the script, how do i execute it in the web page?
3.Can i leverage the firefox/chrome developer console in some way ?
Note:I develop mostly in C,C++ and ruby with very basic experience in HTML and CSS. I have barely used javascript, but i have a vague idea that the browsers use it to handle the content of every page.
My thought process was to somehow execute the script through firefox console, and it will provide me with the results... is that difficult?
edit: since i am not familiar with the web development terms, ill put an a different choice of words...
1.i open up the results page
2.i open the console window of firefox/chrome
3.run my script(dont know what language).
4.i should get the result in the console. i do not want to modify the results page. i just need the result displayed to me(in the console, or to a text file).
Yes it's possible to do what you think using Javascript. Please look up XMLHttpRequest
Here is a way for you to start (in Java):
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class YourClass
{
private WebDriver webDriver = null;
public void open() throws Exception
{
webDriver = new FirefoxDriver();
}
public void close() throws Exception
{
webDriver.quit();
}
public void doStuff() throws Exception
{
WebElement element = webDriver.findElement(By.id(...));
...
// Alternatively, it might be easier to parse the entire
// page-source instead of searching elements in the DOM
String pageSource = webDriver.getPageSource();
...
}
}
You will need to download the following JAR files and add them in your project's class-path:
selenium-java-2.xx.0.jar (currently xx=39)
selenium-server-standalone-2.xx.0.jar (currently xx=39)