java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal - java

I am trying to run a Java project using page factory and page object model, I want to run it on Chrome, IE and on Firefox, but I am not able to run it on Firefox, the browser is not opening, I am getting this error : java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
My Code is :
#BeforeMethod
public static void openBrowser() {
String browser = "";
if (browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "src\\test\\Resources\\BrowserDrivers\\chromedriver.exe");
driver = new ChromeDriver();
}
else if (browser.equalsIgnoreCase("ie")){
System.setProperty("webdriver.ie.driver", "src\\test\\Resources\\BrowserDrivers\\IEDriverServer.exe");
driver = new InternetExplorerDriver();
}
else {
driver = new FirefoxDriver();
}
driver = new FirefoxDriver();
driver.get("http://www.google.com/");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}
#AfterMethod
public static void closeBrowser(){
driver.quit();
}

See e.g. this Github issue, some dependencies can drag in an outdated version of xml-apis which can cause this, a solution that worked for me was to add a specific dependency on xml-apis, e.g. with Maven something like
<dependency>
<groupId>xml-apis</groupId>
<artifactId>xml-apis</artifactId>
<version>1.4.01</version>
</dependency>

Related

Launch Microsoft Edge Chromium browser using selenium

I am trying to launch Microsoft Edge Chromium browser using selenium.
Microsoft Edge chromium Version: Version 79.0.309.65 (Official Build) (64-bit)
Downloading driver file from https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
Using this code to for the same but it is giving unreachable browser Exception and not working.
1.System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Microsoft\\Edge Beta\\Application\\msedgedriver.exe");
EdgeOptions options = new EdgeOptions();
BROWSER=properties.getProperty("BrowserName");
options.setCapability(BROWSER, false);
//DesiredCapabilities m_capability = DesiredCapabilities.edge();
driver= new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), options);
2.DesiredCapabilities m_capability = DesiredCapabilities.chrome();
BROWSER=properties.getProperty("BrowserName");`enter code here`
m_capability.setCapability( BROWSER, "chrome" );
driver = new ChromeDriver();
System.setProperty("webdriver.chrome.driver",
"C:\\edgedriver_win64-1\\msedgedriver.exe");
It is looking like compatibility issue. You can upgrade or downgrade your msedgedriver driver version to make it work.
I will recommend you to use WebDriverManager
WebDriverManager allows to automate the management of the binary
drivers (e.g. chromedriver, geckodriver, etc.) required by Selenium
WebDriver.
maven dependency
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>3.8.0</version>
<scope>test</scope>
</dependency>
Once we have included this dependency, you can let WebDriverManager to manage the WebDriver binaries for you. Now you can set driver instance for Edge.
public class EdgeDevTest {
private WebDriver driver;
#BeforeClass
public static void setupClass() {
WebDriverManager.edgedriver().setup();
}
#Before
public void setupTest() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary(
"C:\\Program Files (x86)\\Microsoft\\Edge Dev\\Application\\msedge.exe");
EdgeOptions edgeOptions = new EdgeOptions().merge(chromeOptions);
driver = new EdgeDriver(edgeOptions);
}
#After
public void teardown() {
if (driver != null) {
driver.quit();
}
}
public void testservice(){
EdgeOptions opt= new EdgeOptions();
opt.setHeadless();
WebDriver driver= new EdgeDriver(opt);
}

The driver executable does not exist: D:\Firefox\geckodriver.exe

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class sasas {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
this is the sample code i am trying. when i run i get the error message as "The driver executable does not exist: D:\Firefox\geckodriver.exe" please help me to proceed. i added the location in environmental variable then too i get this error . PATH i added as D:\Samplecode.
kindly help me
(1) To use gecko driver, make sure you are using Firefox version 55 and above to get better gecko Web-Driver feature support, find out more here
(2) Perhaps, you should downgrade Selenium to a lower version i.e. version 2.53.1. Selenium version 2.53.1 runs perfectly on Firefox 47.0.1 and lower, does not require using web driver API. I have ran your code against this and it worked fine.
public class Sasas {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
Use the relative path:
JAVA
1.- In your project, create the Drivers/Win/Firefox/geckodriver.exe folder and add your .exe
2.- Replace:
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
For:
String path = System.getProperty("user.dir") + "/Drivers/Win/Firefox/Geckodriver.exe";
System.setProperty("webdriver.gecko.driver", path);
Note: using the relative path is the most optimal

Phantom JS is unable to find element (even after giving implicit wait aslo)

I am facing some problem with PhantomJS reg locating the webelements. I have gone through previously answered questions in which 2 possible solutions was given:
Sync Issue-give Implicit wait
Css selector don't work in phantomjs.
I have tried both the solution but my code is still not working.
Code:
public class Headless_phantomJS {
#Test
public void googlesearch() throws InterruptedException
{
File path=new File("C:/Third party softwares/phantomJS/phantomjs-2.1.1-windows/phantomjs-2.1.1-windows/bin/phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[#id='lst-ib']")).isEnabled();
driver.findElement(By.xpath("//input[#id='lst-ib']")).sendKeys("lol");
driver.findElement(By.xpath(".//*[#id='tsf']/div[2]/div[3]/center/input[1]")).click();
}
}
Here is your own code block which executes well with PhantomJS 2.1.1 with some tweaks and some added Sysouts for your convenience:
#Test
public void googlesearch()
{
File path=new File("C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path",path.getAbsolutePath());
WebDriver driver= new PhantomJSDriver();
driver.manage().window().maximize();
driver.navigate().to("https://www.google.co.in/");
System.out.println("inside Test");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Checking if the Search field is Enabled");
driver.findElement(By.name("q")).isEnabled();
System.out.println("Sending lol to Search field");
driver.findElement(By.name("q")).sendKeys("lol");
System.out.println("Clicking on Google Search button Next");
driver.findElement(By.name("btnG")).click();
System.out.println("Google Search Completed");
}

webdriver on safari works well in debug mode, but could not work well when I run it

I test safari browser on Windows with Selenium Webdriver, when in debug mode, my script works well, but when I run it, it could not work well. Does anyone encounter this situation?
public class JustForTestSafari {
public WebDriver driver;
#Test
public void f() {
driver = new SafariDriver();
String baseURL = "http://universitytest.emersonprocess.com/";
String expectedTitle = "ProcessWorld - Your Connection to Global Information";
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get(baseURL);
driver.manage().window().maximize();
String actulTitle = driver.getTitle();
Assert.assertEquals(expectedTitle, actulTitle);
driver.findElement(By.id("_ctl0_mastercontent_username")).clear();
driver.findElement(By.id("_ctl0_mastercontent_username")).sendKeys("***.guo");
driver.findElement(By.id("_ctl0_mastercontent_password")).clear();
driver.findElement(By.id("_ctl0_mastercontent_password")).sendKeys("*******");
driver.findElement(By.id("_ctl0_mastercontent_btn_standardsignin")).click();
}

Running Selenium Webdriver script in Chrome

I am trying to run Selenium Webdriver script in Chrome, have added following lines in my existing script
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\chromedriver.exe");
private WebDriver driver = new ChromeDriver();
I am building my script in Intellij using Java. Not sure why i am getting "can not resolve symbol setProperty". I tried changing JRE and JDK files but nothing has worked. Any help would be appreciated.
Adding code
public class StartCaseJava extends TestCase {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
// Getting Date and Timestamp for Last Name
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyHHmmss");
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
// private WebDriver driver = new ChromeDriver();
// driver = new FirefoxDriver();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private WebDriver driver = new ChromeDriver();
public void testStartCaseJava() throws Exception {
// System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
It's a long shot but should this
C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
maybe be renamed to C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
or are the double back slashes actually a necessary feature?
This might be because of one missing '\' before chromedriver.exe in your setProperty string.
Try using :
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
I am able to run my script using Chrome now. Here is the small sample declaration I used:
class StartCaseJAva {
static WebDriver driver;
public void testcasejava()) {
System.setProperty(path);
driver = new ChromeDriver();
}
}

Categories