removing initialisation logs while using selenium and phantom js - java

i am unable to remove initialisation logs getting displayed while using selenium and phantom js
Below is the code i have used for headless testing
DesiredCapabilities dcap = new DesiredCapabilities();
String[] phantomArgs = new String[] { "--webdriver-loglevel=NONE" };
dcap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
PhantomJSDriver driver = new PhantomJSDriver(dcap);

Related

ChromeDriver with headless raises ElementNotVisibleException

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

Running Tor with Selenium webdriver in Java

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.

Run TOR browser with Selenium WebDriver

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

Invoking WebDriver for Tor from Selenium

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

Exception (org.openqa.selenium.NoSuchWindowException): using Selenium WebDriver with Java in Internet Explorer

While using IE for automation using Selenium Webdriver, I am able to open the URL but finding the element on that page is throwing the following exception:
org.openqa.selenium.NoSuchWindowException: Unable to find element on
closed window (WARNING: The server did not provide any stacktrace
information)
I have tried the driver.switchTo.window() method but it's not working.
I have searched it for hours and I am not getting anywhere.
Here's the code:
public static Selenium selenium;
public static void main(String args[]) {
try {
System.setProperty(
"webdriver.ie.driver",
"D:\\Driver\\IEDriverServer_Win32_2.32.3_latest\\IEDriverServer.exe");
DesiredCapabilities capab = DesiredCapabilities.internetExplorer();
capab.setCapability(
InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,
true);
WebDriver driver = new InternetExplorerDriver(capab);
driver.get("http://www.google.com");
driver.findElement(By.xpath(".//*[#id='addlang']/a[1]")).click();
} catch (Exception e) {
e.printStackTrace();
}
}
Remove capability INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS and manually set your IE protected mode settings to be the same for all zones.
Source:
http://jimevansmusic.blogspot.com/2012/08/youre-doing-it-wrong-protected-mode-and.html
NoSuchElementException is occurred during implementation of InternetExplorerDriver in Selenium WebDriver
case "ie_driver":
//IE CODE
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "https://testvmm6.partnersonline.com/vmm");
cap.internetExplorer().setCapability("ignoreProtectedModeSettings", true);
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"//exe//IEDriverServer1.exe");
cap.setCapability("IE.binary", "C:/Program Files (x86)/Internet Explorer/iexplore.exe");
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setJavascriptEnabled(true);
cap.setCapability("requireWindowFocus", true);
cap.setCapability("enablePersistentHover", false);
The issue that helped me was to set init page (IE 11 both 32 and 64)
private WebDriver getIEDriver() {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, INIT_PAGE);
File file = new File("E:/drivers/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
return new InternetExplorerDriver(cap);
}
The best bet here is to make some tweaks to the registry:
Go to registry edit (regedit from windows run)
Look in your registry under the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\InternetSettings\Zones. Over there, you should see keys number 0-4 . Under these keys 0-4, look for a value named 2500
For all the keys from 0-4, have the same data for value 2500. For example, for key 0 if the value 2500 has data as 3 (hex data), then make the data for value 2500 as 3 for all the other keys (1,2,3,4).
Now try to run the script.

Categories