Saswat Saswat - 5 months ago 38
Node.js Question

How to use phantomJS to open a sitelink using selenium webdriver?

I have installed selenium webdriver via npm by the following method:

C:\xampp\htdocs\testPhantomJS>npm install selenium-webdriver


Then I have put the phantomjs scripts(codes) in the same folder location.

My folder now looks like the image given below:

enter image description here

Requirement:



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.

Script: test.js



var webdriver = require('selenium-webdriver');
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();


I am running the code through command prompt using node.js

C:\xampp\htdocs\testPhantomJS\node_modules\selenium-webdriver >node test.js


After running this, I am getting some error, which is stated below:

Error:



enter image description here

Answer

You have to require the By tool seperately.

var By = require('selenium-webdriver').By;

Or you can use it like this: webdriver.By.func()