tor browser selenium javascript - javascript

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.

Related

Receiving error in selenium (javascript) when opening shadowRoot

I am setting up the first automated tests for a web app I'm working on, and have hit a state I don't understand.
It is a browser app, so I start a very simple static server:
import http from 'http';
let serve = serveStatic(path);
server = http.createServer(function(req, res) {
var done = finalhandler(req, res);
serve(req, res, done);
});
During my testing, I receive an error message HTTP method not allowed
let options = new firefox.Options();
options.headless();
let capabilities = webdriver.Capabilities.firefox().set('acceptInsecureCerts', true);
let driver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(options)
.withCapabilities(capabilities)
.build();
await driver.get('http://127.0.0.1:3030/index.html');
let tab = await driver.findElement(state.By.css('ps-tabpanel'));
tab = await tab.getShadowRoot(); // HTTP method not allowed
On a hunch, I changed this to an HTTPS connection
import http from 'https';
In this case I receive a very different error
await driver.get('https://127.0.0.1:3030/index.html');
// Reached error page: about:neterror?e=nssFailure2&u=https%3A//127.0.0.1%3A3030/index.html&c=UTF-8&d=%20
So my main question is, what am I doing wrong to access the shadowRoot using Javascript Selenium?
For reference
mocha + selenium + firefox
gitpod environment
have an earlier test that simply verifies I can connect to example.com just to prove the connection is working.
In an attempt to work around the error, I switched to the javascript executor. This raised a different error message (cyclical object).
This led me to a different stackoverflow question
https://stackoverflow.com/a/67223939/1961413
According to that answer, this is a known defect in the GeckoDriver/Firefox.
Based on that I switched to the ChromeDriver/Chrome, and was able to find the ShadowRoot.
https://www.npmjs.com/package/chromedriver
let driver = new webdriver.Builder()
.forBrowser('chrome')
.build();
await driver.get('http://127.0.0.1:3030/index.html');
let tab = await driver.findElement(state.By.css('ps-tabpanel'));
tab = await tab.getShadowRoot();

Selenium webdriver TypeError: element.isDisplayed is not a function

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

Loading default chrome profile with webdriver does not automate browser

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

How to use PhantomJS for opening a sitelink using Selenium Webdriver?

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;

Setting service_args for phantomjs in selenium-webdriver for node

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

Categories