How to call JS file through selenium - javascript

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);

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();

Can't turn off Javascript using Selenium

I'm trying to turn off javascript via the profile when opening using Selenium. This has work previously but now I've updated Selenium/Firfox I can't get it to work.
profile = webdriver.FirefoxProfile()
profile.set_preference('javascript.enabled', False)
driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
driver.get("http://www.enable-javascript.com/")
All other settings seem to change while using profile.set_preference() on other option and javascript.enabled exists and is set to True when I look at the Firefox settings about:config. Is it possible Javascript is being set to True after loading the profile or something?
FF version 43.0.3
Selenium version 2.48.0
Any suggestions on why this could be happening?
UPDATE
Adding profile.add_extension("path/to/noscript_security_suite-2.9.xpi"); to the above code with the downloaded extension as #alecxe suggested fixed the issue.
This issue affects selenium starting with 2.46.0, javascript.enabled is being ignored:
Firefox driver 2.46.0 regression - unable to set to non-js
As a workaround, load the noscript addon, see:
How to disable Javascript when using Selenium by JAVA?

Enable JavaScript in PhantomJS with C#

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();

is it possible to develop a browser toolbar that accepts selenium automation

usually selenium does not identify elements on a browser toolbar. But this is for pre-build browser toolbars. Can a browser toolbar ( for FF, IE & Chrome ) be developed in such a way that selenium can be used to identify elements on the toolbar and then perform control actions on it , like clicking on a radio button on the toolbar.
Because Selenium can only control things that JavaScript can control, you need to find out whether JavaScript can control the toolbar. JavaScript has 3 main features. I controls the JavaScript processing, the DOM (document object model) and the BOM (browser object model). There might have been more control given to JavaScript when HTML 5 and CS3 came out, but this is my knowledge of it.
I think you may need to use a cross-browser extension to make your sidebar/toolbar. google-gears/silverlight/adobe-air/jnext. You might need to make the UI using the DOM. I'm not sure. You will need to research.. never done this before.
If the toolbar is part of the BOM (and/or DOM), then you can. Just create a custom command, called Selenium.prototype.doControlToolbar or something and put your JavaScript logic inside of that function (similar to what I have below). Good luck!
Option #1 - if using Selenium IDE:
Specify the user-extensions.js file under Selenium IDE > Options (menu) > Options (menu option) > General Tab, then browse to your file under "Selenium Core Extensions".
Option #2 - if using Selenium RC Server:
If you're not using the IDE and using Selenium RC server with a client driver (like JUnit for example), you must specify the path of the *.js file with the -userExtensions parameter when you start the Selenium RC Server on the command line. But you said you just wanted to use the IDE, so I'd ignore this. It takes quite a bit of other setup to use the Selenium RC server.
java -jar selenium-server.jar -userExtensions user-extensions.js
=======================
I made the following custom command (JavaScript function) in my custom user-extensions.js file.. I had to exit and restart the IDE before it found it. Type everything after the "do" in the "Command" field in the IDE to find the custom command. It looks like it also added a "customAlertAndWait" to the IDE as well.
Code in user-extensions.js file:
Selenium.prototype.doCustomAlert = function(sTarget, sValue) { alert('Target: ' + sTarget + ' ... Value: ' + sValue); };
Selenium IDE command details:
Command: customAlert
Target: custom alert target
Value: custom alert value

Categories

Resources