I am trying to disable javascript in selenium using noscript extension, as suggested here -->How to disable Javascript when using Selenium by JAVA?
But, it looks like it no longer works,
Here is what I have written :
FirefoxProfile profile = new FirefoxProfile();
File extPath = new File("noscript.xpi");
profile.addExtension(extPath);
//profile.setPreference("javascript.enabled", false);
WebDriver driver = new FirefoxDriver(profile);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://enable-javascript.com");
Even tried it by loading a profile that has javascript disabled, that doesn't seem to work either.
Code :
File profileDirectory = new File("Profiles/4hsi6txm.testing");
FirefoxProfile profile = new FirefoxProfile(profileDirectory);
DesiredCapabilities cap = DesiredCapabilities.firefox();
//
cap.setCapability(FirefoxDriver.PROFILE, profile);
WebDriver driver = new FirefoxDriver(cap);
driver.get("http://enable-javascript.com");
I am trying it on Firefox version-43.0.1 and selenium version-2.48.2
Any fix for this ?
Thanks :)
Related
When I write a js webdriver script to navigate to the zoom website, navigate to the page for a call, and click "Launch Meeting" button, I always see a popup:
This site is trying to open Zoom Meetings.
https://us04web.zoom.us wants to open this application
[ ] Always allow us04web.zoom.us to open links of this type in the associated app
https://shariktlt.blog/allow-open-external-protocol-chromedriver/ has the following instructions for java / Chrome, but the setExperimentalOption is not available on webdriver js:
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> prefs = new HashMap<>();
prefs.put(
"protocol_handler.allowed_origin_protocol_pairs",
singletonMap("https://us04web.zoom.us", singletonMap("zoommtg", true))
);
options.setExperimentalOption("prefs", singletonMap("protocol_handler.allowed_origin_protocol_pairs", singletonMap("https://us04web.zoom.us", singletonMap("zoommtg", true))));
WebDriver driver = new ChromeDriver(options);
How can I achieve this for Edge / TS or JS?
The following worked for me with webdriver 4.0.0, edgedriver 105:
const edgeOptions = new Options()
edgeOptions.setUserPreferences({
"protocol_handler": {
"allowed_origin_protocol_pairs": {
"https://us04web.zoom.us": { "zoommtg": true }
}
}
})
const webDriver = await new Builder().setEdgeOptions(edgeOptions).build()
i've been trying for hours launching a selenium web browser and now it's done, i can't put it in headless mode ( want it to perform tasks in background ). My electron app is a simple quick start with a button launching a Selenium webdriver. Here's my code :
document.getElementById("test").onclick = function () {
require('chromedriver');
var webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
//setting chrome options
var chromeOptions = {
'args': ['--headless']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
driver.get('http://www.google.com/');
};
What it makes : When you press the test button, selenium browser is launched and appears in my screen instead of being headless or in background :(
It seems like it doens't take in consideration the args of the chromeOption variable. I tried to put many different flags with many different syntaxes but noone of them worked. Does anyone have a solution please ?
I'm getting screenshots of webpages using Chromedriver. My code works well.
I'm now attempting to remove the ugly scrollbars. Would it be possible to inject CSS into the page? I have seen a couple of similar questions, which do hint that this is possible ("You can also make Chromedriver open the url in a popup without scrollbars. You can do this using some Javascript.") but doesn't show how this is possible.
Has anybody got any idea as to how to do this?
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Size = new Size(1100, 1100);
driver.Navigate().GoToUrl("http://google.com/");
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
//Temp Img
string screenshot = ss.AsBase64EncodedString;
byte[] screenshotAsByteArray = ss.AsByteArray;
ss.SaveAsFile(fileNameTemp, ScreenshotImageFormat.Jpeg);
ss.ToString();
// Create a driver instance for chromedriver
IWebDriver driver = new ChromeDriver();
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
driver.Manage().Window.Size = new Size(1100, 1100);
driver.Navigate().GoToUrl(hehe[i]);
js.ExecuteScript("return document.body.style.overflow = 'hidden';");
This worked!
I wrote a Selenium test with java that I launched with the FirefoxDriver and it is executing fine in Firefox browser.
Then I am replacing FirefoxDriver with HtmlunitDriver like this:
driver = new FirefoxDriver();
with
driver = new HtmlUnitDriver(true);
But then I got this error :
It's missing ';' Before an instruction (http://local.project/bundles/app/js/socket.js#1)
This is the socket.js file :
class SocketHandler {
constructor(url) {
this.url = url;
this.session = null;
}
....
}
I suspect that it doesn't recognize the class declaration. Any idea how to correct that please?
You don't even need to use PhantomJs. As PhantomJs is not so much maintain these days. You can use chromedriver in headless mode.
you just need to add options as headless as below :-
chromeOptions.addArguments("--headless");
Please find complete code below:
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://google.com");
While if still you want to use phantomjs. then first download phantomjs binary from below location :-
http://phantomjs.org/download.html
Now use below code :-
System.setProperty("phantomjs.binary.path","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\phantomjs\\phantomjs.exe");
DesiredCapabilities capabilities = null;
ArrayList<String> cliArgsCap = new ArrayList<String>();
capabilities = DesiredCapabilities.phantomjs();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
capabilities.setCapability("takesScreenshot", true);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=2" });
driver = new PhantomJSDriver(capabilities);
driver.get("https://www.google.co.in/");
Hope it will help you :)
I can't find FirefoxDriver's option, which equivalents --always-authorize-plugins in ChromeDriver.
Does FirefoxDriver contain an equivalent option?
P.S.
--always-authorize-plugins enables all plugin in ChromeDriver.
I find a solution for my problem.
My Solution:
close firefox
open firefox with flag -p. win + r => "firefox.exe + -p"
create a new firefox profile
find full path to the profile
%APPDATA%/Mozila/Firefox/Profiles/[profileName]
then I start webDriver with the profile
var until = require('selenium-webdriver').until,
firefox = require('selenium-webdriver/firefox');
var options = new firefox.Options();
options.setProfile([fullPath]);
var driver = new firefox.Driver(options);
driver.get('www.google.com');
driver.wait(until.titleIs('webdriver - Google Search'), 20000);
driver.quit();
activate need plugins while firefox is open.
restart webDriver.
Try "plugin.state.java" = 2 in FireFox Profile