I am trying to use headless Webkit of PhantomJS to open google.com through Selenium Webdriver but when I execute the following code system, some error occurs. Have I missed something?
var webdriver = require('selenium-webdriver');
var By = require('selenium-webdriver').By;
var driver = new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.phantomjs())
.build();
var baseUrl = 'http://google.com/';
driver.get(baseUrl);
driver.findElement(By.name('q')).sendKeys('webdriver');
driver.findElement(By.name('btnG')).click();
driver.wait(until.titleIs('webdriver - Google Search'), 1000);
driver.quit();
But I am getting a new error:
Require until first:
var until = require('selenium-webdriver').until;
Related
I'm using selenium with Tor but it's not working , i saw that there is a library for doing that but only with python . Can this be done with javascript ? I tried that but it doesn't work.
const {Builder, By, Key, until} = require('selenium-webdriver');
var driver = new Builder()
.forBrowser('tor')
.build();
driver.get('https://www.google.com')
As far as I remember, this can be done only from Java and Python.
I had trouble getting the latest geckodriver (0.21.0) and Selenium (3.13.0) to fetch a web page after launching the Tor Browser Bundle. I think it may be incompatibilities with the older Firefox version Tor uses and the geckodriver but am not sure.
If you're just trying to use selenium-webdriver to use the Tor network, try this:
const webdriver = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
var options = new firefox.Options();
options.setPreference('network.proxy.type', 1) // manual proxy config
.setPreference('network.proxy.socks', '127.0.0.1')
.setPreference('network.proxy.socks_port', 9050)
.setPreference('network.proxy.socks_remote_dns', true) // resolve DNS over Tor
.setPreference('network.proxy.socks_version', 5)
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
driver.get('https://example.com/')
You will need to run Tor using the expert bundle or install and run it natively.
Here's what I tried for actually getting Tor Browser to automate. It launches everything correctly but never navigates to the page.
const webdriver = require('selenium-webdriver');
const firefox = require('selenium-webdriver/firefox');
var options = new firefox.Options();
options.setBinary('/home/me/Desktop/tor-browser_en-US/Browser/start-tor-browser');
options.addArguments('--detach');
(async function run() {
let driver = await new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.build();
await driver.get('https://example.com/')
})();
Just to re-iterate, this second example isn't working. On Mint 18 and Tor Browser 7.5.6 (FF ESR 52.9.0) it launches Tor and the browser just fine, but will not navigate to a page.
I have an error:
TypeError: element.isDisplayed is not a function
When executing the following code:
var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;
var driver = new webdriver.Builder()
.forBrowser('chrome')
.usingServer('http://localhost:4444/wd/hub')
.build();
driver.get('https://www.test.com');
driver.wait(until.elementIsVisible(By.id('someButton')), 5000);
This is on my local machine using https://www.npmjs.com/package/selenium-webdriver and kicking off a server with:
webdriver-manager start
My spec:
Mac oSX Sierra 10.12.6
Chrome v60
The site I'm developing on is using AJAX to load pages to this could make a difference?
Problem
until.elementIsVisible(..) needs a WebElement an not a Locator as a Argument.
Solution
Writing
driver.wait(until.elementIsVisible(driver.findElement(By.id('someButton'))), 5000);
instead of
driver.wait(until.elementIsVisible(By.id('someButton')), 5000);
will solve the Problem.
More Infos
http://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/lib/until.html
https://github.com/SeleniumHQ/selenium/issues/2935
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 :)
In the following topic: Load Default chrome profile with Webdriverjs Selenium, the solution listed there works fine but nothing happens when the browser is loaded, it's not loading the webpage. Here is the code:
var webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome'),
assert = require('assert'),
By = webdriver.By,
until = webdriver.until;
var o = new chrome.Options();
o.addArguments("user-data-dir=/Users/rup/Library/Application Support/Google/Chrome/");
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(o).build();
driver.get('http://www.facebook.com');
driver.wait(function() {
return driver.getTitle().then(function(title) {
console.log("title: " + title);
return true;
});
}, 5000);
I have the feeling it's because the "Chrome browser automation" extension isn't being loaded with that profile. I am new to webdriver so not sure how to include it, and if that would even solve the problem. i am using Webdriver JS (javascript).
I need to be able to run phantomjs with the following arg:
--ignore-ssl-errors=true
The page I'm testing uses a self-signed cert so I need the arg to open the page. I'm trying to pass the arg in webdriver using the snippet below:
capabilities = webdriver.Capabilities.phantomjs();
capabilities.set('service_args', '--ignore-ssl-errors=true');
driver = new webdriver.Builder().
withCapabilities(capabilities).
build();
Is the correct way to pass the service_args? I actually hope not since I can't load my test page. I can open the page by running:
phantomjs --ignore-ssl-errors=true myTest.js
Here is the code in myTest.js
var page = new WebPage();
page.open('https://my.somefaketestpage.com/', function (status) {
just_wait();
});
function just_wait() {
setTimeout(function() {
page.render('screenshot.png');
phantom.exit();
}, 2000);
}
The correct answer is:
caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"});
driver = new PhantomJSDriver(caps);
documented here: https://github.com/detro/ghostdriver/issues/233
In case someone will need it for facebook/php-webdriver CLI arguments can be passed to PhantomJS in a following manner:
$driver = RemoteWebDriver::create('http://localhost:4444/wd/hub', [
WebDriverCapabilityType::BROWSER_NAME => WebDriverBrowserType::PHANTOMJS,
WebDriverCapabilityType::PLATFORM => WebDriverPlatform::ANY,
'phantomjs.cli.args' => ['--ignore-ssl-errors=true']
]);
Reading this I got really confused, as the accepted answer is in Java, and the GhostDriver constants and stuff aren't present. For those who are also confused, this worked for me:
var webdriver = require('selenium-webdriver'),
Capabilities = webdriver.Capabilities;
var capability = Capabilities
.phantomjs()
.set('phantomjs.cli.args', '--ignore-ssl-errors=true');
var driver = new webdriver
.Builder()
.withCapabilities(capability)
.build();