Enable JavaScript in PhantomJS with C# - javascript

I've used PhantomJS in an C# application and it's not executing JavaScript even though the property PhantomJSDriver.Capabilities.IsJavaScriptEnabled is true. The simple page below still executes the content of the noscript tag. How can I make PhantomJS execute JavaScript?
I've added Selenium and PhantomJS to my VS2012 solution via NuGet:
PM> Install-Package Selenium.WebDriver
PM> Install-Package PhantomJS
I've created a simple HTML page to demonstrate that JavaScript is not enabled:
<html>
<body>
Go to another page
<noscript>
No JavaScript!
</noscript>
</body>
</html>
I've used the PhantomJSDriver. src displays "No Javascript!"
public class Program
{
public static void Main(string[] args)
{
var phantomDriver = new PhantomJSDriver();
phantomDriver.Url = #"C:\page.html";
var src = phantomDriver.PageSource;
}
}

JavaScript is by default enabled when using PhantomJS. In fact I'm not aware that any WebDriver starts their browser without JavaScript by default.
To make sure that JavaScript is enabled, you can check
var phantomDriver = new PhantomJSDriver();
var enabled = phantomDriver.Capabilities.IsJavaScriptEnabled;
You can also check experimentally that the JavaScript is running by taking a screenshot and checking that the noscript block is actually not shown. So when the screenshot (phantomDriver.GetScreenshot();) is blank in your case then it works.
It is by the way a bad idea to disable JavaScript for the PhantomJSDriver, because many operations of the WebDriver protocol are implemented in JavaScript. Disabling JS would effectively disable the driver.

PageSource is not supposed to execute JavaScript, it gets the source of the page last loaded by the browser, so it includes everything in your HTML file.
To see the actual state of the page use
phantomDriver.GetScreenshot();

Related

Copy to clipboard in jupyter notebook

I'd like to implement a clipboard copy in a jupyter notebok.
The jupyter notebook is running remotely, thus I cannot use pandas.to_clipboard or pyperclip and I have to use javascript
This is what I came up with:
def js_code_copy(content)
return """
var body = document.getElementsByTagName('body')[0];
var tmp_textbox = document.createElement('input');
body.appendChild(tmp_textbox);
tmp_textbox.setAttribute('value', '{content}');
tmp_textbox.select();
document.execCommand('copy');
body.removeChild(tmp_textbox);
""".format(content=content.replace("'", '\\'+"'"))
Note that the code does what it's supposed to if I run it in my browser's console.
However, if I run it in jupyter with:
from IPython.display import display, Javascript
content = "boom"
display(Javascript(js_code_copy("Copy me to clipboard")))
Nothing works,
Any ideas ?
For security reasons, your browser disables the use of document.execCommand if the method wasn't called as a result of a user action, e.g clicking a button.
Since you're injecting and running Javascript on the page, this is not considered a user action.
You could try using selenium and phantomJS to run the code in a headless browser in the background.

C# script that detect already opened website and click buttons and inserta data

I would like to write simple scripts which after I have already opened site ( I dont wanna script to open it) press two buttons and insert data in comment section after pressing f.ex. 'g' button. I am completly new in that kind of programming so any help will be nice( also link to good tutorials).
webBrowser1.Document.GetElementById("User").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("but").InvokeMember("click");
I am aware of those 2 functions i will use but how to instantiate them on already opened page by pressing a button? (If thats important deafult used browser is opera).
You should use something like Selenium (http://www.seleniumhq.org/) which is a browser automation framework.
Selenium scripts can be written in many languages (including c#) and the scripts can be run on a variety of browsers. There is even browser plugins for creating scripts my recording a macro - no code required!
This is much more robust that using a browser control embedded in an app as that is only a cut down version of internet explorer I believe.
This is a rough sample of selenium in c#
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
Driver = new InternetExplorerDriver(options);
Driver.Navigate().GoToUrl("yourURL");
Driver.FindElement(By.Id("User")).SendKeys("<your text>");
Driver.FindElement(By.Id("but")).Click();

Execute javascript without webview in Android

I'm trying to execute a JS fonction in my Android app.
The function is in a .js file on a website.
I'm not using webview, I want to execute the JS function because it sends the request i want.
In the Console in my browser i just have to do question.vote(0);, how can I do it in my app ?
UPDATE 2018: AndroidJSCore has been superseded by LiquidCore, which is based on V8. Not only does it include the V8 engine, but all of Node.js is available as well.
You can execute JavaScript without a WebView. You can use AndroidJSCore. Here is a quick example how you might do it:
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://your_website_here/file.js");
HttpResponse response = client.execute(request);
String js = EntityUtils.toString(response.getEntity());
JSContext context = new JSContext();
context.evaluateScript(js);
context.evaluateScript("question.vote(0);");
However, this most likely won't work outside of a WebView, because I presume you are not only relying on JavaScript, but AJAX, which is not part of pure JavaScript. It requires a browser implementation.
Is there a reason you don't use a hidden WebView and simply inject your code?
// Create a WebView and load a page that includes your JS file
webView.evaluateJavascript("question.vote(0);", null);
For the future reference, there is a library by square for this purpose.
https://github.com/square/duktape-android
This library is a wrapper for Duktape, embeddable JavaScript engine.
You can run javascript without toying with WebView.
Duktape is Ecmascript E5/E5.1 compliant, so basic stuff can be done with this.

How To Show Progress Bar When The Java Applet Load On Web Browser

I have a Jar File which run the applet. This Jar File is called by Call_Applet_Jar.html file which is in www directory. The Call_Applet_Jar.html file has a javascript and html codes. The Jar File is called by html codes which are in Call_Applet_Jar.html. The applet opens too late on browsers. For example applet opens in five seconds with unix o.s's browsers despite this the applet opens in 15 seconds with windows o.s's browsers. Namely applet run quick on unix's browsers but it can not run quick on windows's browsers. So that I want to show progress bar while the applet is loading. But I can't understand when the applet becomes fully operational on browser. Namely I must know loading time's start and finish point to add progress bar. How to understand time when the applet becomes fully operational on browser with Javascript or HTML. The progress bar must disappear when the applet becomes full operational on web browsers. How can i know this time with javascript or html?Any idea? Thanks in advance ..
Please see Displaying a Customized Loading Progress Indicator:
A Java applet can display a customized loading progress indicator that shows the progress of download of the applet's resources as well as other applet specific data.
You can't know with javascript or html when java applet loaded completely on browsers. To load applet on browsers.
1--> JVM should runned.
2-->The jar should downloaded and ran.
3-->The ready() method should access to page
All of them are not understood by javascript or html. You must call javascript function from java applet when applet is loaded completely on browser. Namely java applet should give notice to javascript side. In this way you can understand the applet is loaded completely on browser. The example code is below.
package example_package;
import netscape.javascript.*;
public class Example_Applet_Class extends Applet {
public void start() {
try {
JSObject calling_object = JSObject.getWindow(this);
//to call javascript method when the applet is loaded on browser completely..
String passing_argument = "applet jar is downloaded";
calling_object.call("javascript_function" , new Object[] {passing_argument});
//or you can set the javascript object when applet is loaded on browser completely..
String assign_value_js_object = "loaded";
calling_object.setMember("js_object",assign_value_js_object);
} catch (JSException jse) {
jse.printStackTrace();
}
///**//Javascript Side//**///
<head>
<title>JavaScript Side</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
<script language="javascript">
var js_object= "";
if(js_object == "loaded")
{
//The code which should ran when the applet is loaded completely on browsers.
}
//Called from java Applet
function javascript_function(argument_came_from_java_applet)
{
//The code which should ran when the applet is loaded completely on browsers.
}
</script>
</head>
<body>
...
</body>

How to call JS file through selenium

Is there a way I can run my JS file through selenium Web driver?
I'm given to disable and enable JS script in my firefox browser when my tests are running. Is it possible to do that?
Currently, I set preferences in firefox profile and launch the browser to disable javascript, it is possible to do at runtime?
Why are you disabling the Javascript? You need to enable them in order to execute your own script. You want to use JavascriptExecutor. You can do something like below.
String fileContents = Files.toString(new File("c:\\fullpathtoyourJS\\test.js"), Charsets.UTF_8);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(fileContents);

Categories

Resources