Selenium WebDriver can find element in Chrome but not in FireFox - java

I'm still new to working with FireFox and using the Selenium Driver. I wrote the following driver code in Java as part of a CucumberTest. I have the ChromeDriver section commented out. When I comment out the FirefoxDriver and run this using the ChromeDriver line it works as expected. However, when I try to run the exact same code swapped to the FirefoxDriver, I get to my first when statement and I get a nullPointerException at the first find element by id line. Is there a workaround?
#When("^User enters \"(.*)\" and \"(.*)\"$")
public void user_enters_UserName_and_Password(String realUsername, String realPassword)
throws Throwable {
System.out.println(realUsername + " " + realPassword);
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(realUsername);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(realPassword);
driver.findElement(By.id("loginbutton")).click();
}
EDIT: The issue was in how I was calling the driver. I corrected how I initilzed my driver to this and it worked. Something about how I was calling it before caused it to get discarded so it was null by the time it came to this step.
The issue was in how I was instantiating the Firefox driver in a different file. This is what worked ' String marionetteDriverLocation = "c://Features//geckodriver.exe";
System.setProperty("webdriver.gecko.driver", marionetteDriverLocation);
//create firefox profile
FirefoxProfile myProfile= new FirefoxProfile();
// This will set the true value
myProfile.setAcceptUntrustedCertificates(true);
//setup the driver with profile settings for security issues.
driver = new FirefoxDriver(myProfile);'

Related

Set firefox and chrome driver for selenium in java?

I am facing a problem with selenium web driver in java, it says that "The path to the driver executable must be set by the webdriver.gecko.driver system property" you can see it below, but I have set everything as usual I do.
You just need to change the execution flow.
You see, you get the exception because you create FirefoxDriver first and THEN you set the property. It should be in reverse order.
First, set property and THEN initialize WebDriver:
public class EntryPoint {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "path/to/executable.exe");
WebDriver driver = new FirefoxDriver();
}
}
Assuming you are on Windows:
1. Try replacing "/" with "\" on your path.
2. If the previous step did not work, try running Intellij as an admin. The program might not have the right permissions to execute anything on that folder.

Selenium with Java error :"File path wcxChrome.crx needs to be absolute in key ..."

I'm new to Selenium, and I am doing some SeleniumDriver practices on Java, following the example code on "https://saucelabs.com/resources/articles/getting-started-with-webdriver-selenium-for-java-in-eclipse".
I followed the instructions but I got an error. Here is the code block:
#Test
public void site_header_is_on_home_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
WebDriver browser = new ChromeDriver();
browser.get("https://www.saucelabs.com");
WebElement href = browser.findElement(By.xpath("//a[#href='/beta/login']"));
assertTrue((href.isDisplayed()));
browser.close();
}
The error message is:
[17200:25400:0515/151552.506:ERROR:external_registry_loader_win.cc(152)]
File path wcxChrome.crx needs to be absolute in key
Software\Google\Chrome\Extensions\mjdepfkicdcciagbigfcmdhknnoaaegf
Can someone help me fix this? Many thanks!
It appeared that the problem was because I gave the link to Chrome, not the ChromeDriver. I changed the path to ChromeDriver and it worked fine.

Error :"java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property" is displayed

Following is my code snippet :
public class FindAllElementsFromTable {
WebDriver driver;
#BeforeClass
public void beforeClass(){
System.setProperty("Webdriver.chrome.driver","C:\\Selenium Drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
and some tests after this.
When executed,getting above exception.
I tried with forward slash i.e. "C:/Selenium Drivers/chromedriver.exe" as well, but it makes no difference. I checked other answers, but I haven't made any of those mistakes like initialising the chromedriver twice/wrong place/setting property after WebDriver initialization/wrong driver name etc.
Can you help me understanding the exact issue?
You have a typo in your setProperty line.
It should be webdriver not Webdriver
System.setProperty("webdriver.chrome.driver","C:\\Selenium Drivers\\chromedriver.exe");

Firefox 48 and Selenium timeout issue

I am unable to run my tests on Firefox 48 using latest Selenium versions (2.53, Selenium 3 beta).
Please explain the configuration needed and the code to use to successfully run tests on Firefox 48. I have pointed to the geckodriver and tried to initialise the same in my code.
Code:
System.setProperty("webdriver.gecko.driver","E:\\Work\\Selenium\\geckodriver-v0.9.0-win64\\geckodriver.exe");
WebDriver driver = null;
driver = new MarionetteDriver();
Getting the below:
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Worked for me:
System.setProperty("webdriver.gecko.driver", "PATH TO GECKO DRIVER");
DesiredCapabilities ffCapabilities = DesiredCapabilities.firefox();
ffCapabilities.setCapability("marionette",true);
WebDriver driver = new FirefoxDriver(ffCapabilities);
You need to write DesiredCapabilities. Add this line before driver initialization.
DesiredCapabilities cap = DesiredCapabilities.firefox()
You can download geckodriver from the link
https://github.com/mozilla/geckodriver/releases
Then save the file in your local system. unzip the file and change the application name as "wires.exe".
Then specify the path upto wires.exe in the code.
add selenium-2.53.0 jar files.
Try below code to start working on FF 47.0 or above.
package com.marionette.programs;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
public class HandleLatestFirefox {
public static void main(String[] args) {
String currentDir = System.getProperty("user.dir");
System.out.println(currentDir);
//String marionetteDriverLocation = currentDir + "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe";
System.setProperty("webdriver.gecko.driver", "G:\\ravik\\Ravi-Training\\Selenium\\Marionette for firefox\\wires.exe");
WebDriver driver = new MarionetteDriver();
driver.get("https://www.google.co.in/webhp?hl=en&sa=X&ved=0ahUKEwjdgc21jJHOAhVCvY8KHZ4aCdcQPAgD");
System.out.println("marionette working fine....");
}
}

Passing options to chrome driver selenium

I am trying to disable the output to the console for chrome. If I pass the --start-maximized option it works fine. I may have the wrong command?
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--silent"));
chrome = new ChromeDriver(_chromeservice,capabilities);
I also tried
ChromeOptions options = new ChromeOptions();
options.addArguments("silent");
chrome = new ChromeDriver(options);
Output
Started ChromeDriver port=26703 version=23.0.1240.0
log=/Brett/workspace/TestNG/chromedriver.log
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending sends
[1214/161331:ERROR:ipc_sync_channel.cc(378)] Canceling pending
sendsBlockquote
Hinted by this Chromedriver ticket (about the silent option), I looked in the source of ChromeDriverService.java, and found a reference to "webdriver.chrome.logfile".
After adding -Dwebdriver.chrome.logfile="/dev/null" to my java command, the logs became readable again: The usless ChromeDriver logs were gone, while theSystem.out.println calls and exceptions are still shown in the console.
I start java with the following parameters (Linux / Mac):
DIR=path/to/dir/containing/selenium/and/stuff
cd "$DIR" && java -cp "$DIR\
:$DIR/output\
:$DIR/bin/selenium-server-standalone-2.33.0.jar" \
-Dwebdriver.chrome.driver="$DIR/bin/chromedriver" \
-Dwebdriver.chrome.args="--disable-logging" \
-Dwebdriver.chrome.logfile="/dev/null" \
AllTests
If you're on Windows:
set DIR=path\to\dir\containing\selenium\and\stuff
cd "%DIR%" && java -cp "%DIR%;%DIR%\output;%DIR%\bin\selenium-server-standalone-2.33.0.jar" ^
-Dwebdriver.chrome.driver="%DIR%\bin\chromedriver.exe" ^
-Dwebdriver.chrome.args="--disable-logging" ^
-Dwebdriver.chrome.logfile=NUL ^
AllTests
Explanation for the composition of my classpath (-cp): My tests are located in a directory at "$DIR/output". The Selenium jar file is placed in "$DIR/bin/selenium-server-standalone-2.33.0.jar". "AllTests" is the name of my class containing public static void main(String[] args) - this launches my tests.
The other parameters are self-explanatory, adjust it to your needs. For convenience (used in a shell/batch script), I've declared the common directory in a variable DIR.
When I was setting chrome up with
selenium-chrome-driver-2.48.2.jar
chromedriver 2.20
selenium-java-2.48.2.jar
none of the above answers worked for me,
Since I see some of the answers are a few years old, I will post what worked for me.
ChromeOptions chromeOptions = setupChromeOptions();
System.setProperty("webdriver.chrome.logfile", "\\path\\chromedriver.log");
System.setProperty("webdriver.chrome.driver", "\\path\\chromedriver.exe");
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver(chromeOptions);
Try "--disable-logging" instead.
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--disable-logging"));
chrome = new ChromeDriver(_chromeservice,capabilities);
As of Selenium 3 at least, you can use ChromeDriverService and its inner class Builder to be able to launch the driver in silent mode.
A oneliner for that:
new ChromeDriver(new ChromeDriverService.Builder().withSilent(true).build());
The constructor is straight-forward, you create a new service builder setting silent to true (that's the critical part) and you finally build it into a ChromeDriverService which is required by ChromeDriver's constructor.

Categories