Im currently trying to execute TOR 6.0.4 with Selenium WebDriver (JAVA) 2.53 and Firefox 43.0. I've followed the instructions from this post Using Selenium WebDriver with Tor but Im getting an error while loading the profilePath of TOR to the Firefox Binary. I've seen that is possible to lunch TOR by loading the TOR profile.default archive to the firefox binaty, but Im getting a Driver info: driver.version: unknown, when instantiating the binary with the profile. I've tried to change the firefox version and still. Below the code where I start the driver. Im also using Windows.
String torPath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Start Tor Browser.exe";
String profilePath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/webhp?complete=1&hl=en");
See below the exception thrown:
Exception in thread "main" org.openqa.selenium.WebDriverException: Specified firefox binary location does not exist or is not a real file: C:\Users\Jose Bernhardt\Desktop\Tor Browser\Start Tor Browser.exe
Seems that I was loading the Tor.exe and instead I had to load the firefox.exe from the Tor archive. I change my path to this and is working. Also fix that I was not sending the profile and the binary to the driver constructor
"C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\firefox.exe"
FirefoxDriver driver = new FirefoxDriver(binary, torProfile);
System.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");
String torPath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS,options);
WebDriver driver = new FirefoxDriver(options);
Till now Tor browser is supported using the Mozilla Firefox
Here is the code below:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\eclipse-workspace\\TorSelenium\\src\\main\\resources\\geckodriver.exe");//sa used for the firefox
String torBinaryPath = "C:\\Users\\user\\OneDrive\\Desktop\\Lk's stuff\\Tor Browser\\Browser\\firefox.exe"; //It is inside the tor browser's folder
Runtime runTime = Runtime.getRuntime();
Process torProcess = runTime.exec(torBinaryPath + " -n");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
WebDriver driver;
driver = new FirefoxDriver(firefoxOptions);
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebDriverWait wait;
wait = new WebDriverWait(driver, 30);
JavascriptExecutor js = (JavascriptExecutor) driver;
//added end
System.out.println(ean);
//Thread.sleep(100);
//driver.navigate().to("https://www.google.com/?hl=en");
//driver.navigate().to("https://duckduckgo.com/?q=d&ia=web");
driver.navigate().to("https://www.swiggy.com");
If you want to change the tor identity then you have to restart the tor using and again start above code:
Runtime.getRuntime().exec("taskkill /f /IM firefox");
Runtime.getRuntime().exec("taskkill /f /IM firefox.exe");
if(torProcess.isAlive()) {
System.out.println("destroying tor");
torProcess.destroy();
}
if(torProcess.isAlive()) {
System.out.println("forcebly destroying tor");
torProcess.destroyForcibly();
}
Related
Here is my current code to launch browser without any proxy:
properties = getGridProperties();
DesiredCapabilities capabilities = null;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("layout.css.devPixelsPerPx","0.9");
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
options.addPreference("dom.webnotifications.enabled", false);
if (properties.containsKey("hub.gecko.driver.path"))
System.setProperty("webdriver.gecko.driver", properties.getProperty("hub.gecko.driver.path"));
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
if (browserType.equalsIgnoreCase("oldfirefox")) {
capabilities.setCapability("marionette", false);
capabilities.setCapability("platform", "LINUX");
options.merge(capabilities);
}
printInfo("initRemoteWebDriver:started:" + System.currentTimeMillis());
capabilities.setCapability("idleTimeout", 150);
String nodeURL = "http://" + properties.getProperty("grid.hub.host") + "/wd/hub";
capabilities.setCapability("idleTimeout", 150);
capabilities.setCapability("name", this.getClass().getCanonicalName());
driver = new RemoteWebDriver(new URL(nodeURL), capabilities);
setDriver(driver);
getDriver().manage().window().maximize();
printInfo("****Firefox browser launched***");
printInfo("initRemoteWebDriver:finished:" + System.currentTimeMillis());
Desired proxy details to be set:
HTTP_PROXY = 'http://www-proxy.us.abc.com:80'
HTTPS_PROXY = 'http://www-proxy.us.abc.com:80'
What is the simplest way to do this without changing the current code too much?
Hi please try this one
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://www-proxy.us.abc.com:80");
capabilities.setCapability(CapabilityType.PROXY, proxy);
I hope it will work for you.
Thanks
Please refer Selenium documentation - https://www.selenium.dev/documentation/en/webdriver/http_proxies/
An alternative would be to use JVM flags. That way you don't have to change your code at all.
java -Dhttp.proxyHost=http://www-proxy.us.abc.com -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=”localhost|127.0.0.1|10.*.*.*”
I am new in selenium. I need a browser without a graphical interface because the project will start with Jenkins. I decided to use ChromeDriver in Headdless mode.
When I use ChrimeDriver in normal mode, I can click on all elements:
WebDriver driver = new ChromeDriver();
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click(); // is ok
}
But when I use Headdless mode then I have: ElementNotVisibleException: element not visible. What could be wrong? Thank you for every clue.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click();//ElementNotVisibleException dont see next li elements
//div[#id='accordian']/div/ul/li
}
While working with Selenium Client v3.11.0, Chrome Driver v2.36 and Chrome Browser v65.x in Headless Mode, you need to pass the following arguments through an instance of ChromeOptions Class while initializing the WebDriver and the Web Browser as follows :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in");
You need to pass "--headless", chrome option like below.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
For entire list of chrome options, refer the following URL. It explains every command line switches in detail.
https://peter.sh/experiments/chromium-command-line-switches/
While working with headless mode, I encountered org.openqa.selenium.UnhandledAlertException due to not handling popping out of Alert Boxes. So it is better if you could handle the alert boxes.
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
I try to write a selenium test With HtmlUnitDriver it is:
HtmlUnitDriver driver = new HtmlUnitDriver();
//driver.setJavascriptEnabled(true);
driver.get(localhostURLAPI);
System.out.println("source of the page now is " + driver.getPageSource());
but in the result I have juste the HTML of index.html with an empty
any help is welcome
You need to change the line:
From :
HtmlUnitDriver driver = new HtmlUnitDriver();
To :
WebDriver driver = new HtmlUnitDriver();
I'm a bit new to selenium and I was looking to use it to run various automated tasks through Tor. I'm on Windows 10 using Java through Eclipse with the Selenium packages as referenced libraries. I've checked out several other tutorials and forum threads detailing how to set up Tor in selenium. I started out trying to use the following code:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
WebDriver driver = new FirefoxDriver(binary, torProfile);
driver.get("https://www.google.com");
Using that code produces this error:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
I tried referencing this thread for a solution. I then used the following code based off of that thread:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
torProfile.setPreference("webdriver.load.strategy", "unstable");
try
{
binary.startProfile(torProfile, new File(profilePath), "");
}
catch (IOException e)
{
e.printStackTrace();
}
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
WebDriver driver = new FirefoxDriver(binary, profile);
driver.get("https://www.google.com");
Using that code launches Tor for me; however, I still receive the same error as before and the driver cannot control that Tor window.
I would appreciate any help or advice.
I need a Webdriver instance of Tor to be able to launch google.com in tor from selenium, java code in Windows and be able to search for a Target String
My code is below
String torPath = "..Installations\\Tor\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "..Installations\\Tor\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");
This results in a blank Tor Browser page opening. It does not load google.com as desired. I know that the profile is valid/compatible because I can successfully start the browser and profile with:
binary.startProfile(profile, profilePath, ""));
I have looked at similar questions but did not get satisfactory answer.
Can it be done ? If yes , how ?
I am looking for Java code.
FirefoxDriver firefoxDriver;
File torProfileDir = new File("C:\\tor\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
FirefoxBinary binary = new FirefoxBinary(new File( "C:\\tor\\Tor Browser\\Browser\\firefox.exe"));//C:\tor\Tor Browser\Browser
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
torProfile.setPreference("webdriver.load.strategy", "unstable");
binary.startProfile(torProfile, torProfileDir);
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
use this code