Webdriver can login in Chrome, not in firefox: 

2019-01-28 16:35发布

问题:

I have 2 classes belonging to the same package in a java testNG project and I have declared 'webdriver driver' public static in class A. In that class, chrome launched, url opened, username and password were entered and login button clicked. Worked fine using @BeforeClass annotation.

I copied the same code into a class B and changed browser instance to firefox whilst still declaring 'webdriver driver as public static. Firefox Launched, URL opened, username and password were entered but login button did not click or submit. Test failed with the error:

org.openqa.selenium.JavascriptException: Error: Unable to find owning document.

I have never come across this error and have no idea what 'owning document' is being referred to.I am suspecting it has something to do with the access level for either or both classes. Below is an extract from the 2 classes. Am I missing something?

*

package com.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TheChrome {
public static WebDriver driver;


@BeforeClass(alwaysRun = true)
public void launchBrowser() {

driver = new ChromeDriver();
driver.get("http://www.example.com");
driver.manage().window().maximize();

@Test
public void verifyLogin() throws InterruptedException {

driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
Thread.sleep(3000);
driver.findElement(By.id("loginButton")).click();

*

package com.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TheFirefox {

public static WebDriver driver;


@BeforeClass(alwaysRun = true)
public void launchBrowser() throws InterruptedException {

driver = new FirefoxDriver();

driver.get("http://www.example.com");
driver.manage().window().maximize();
Thread.sleep(3000);           
}   


@Test
public void verifyLogin() throws InterruptedException {

driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
Thread.sleep(3000);
driver.findElement(By.id("loginButton")).click();

回答1:

edit: See this very relevant question and answer

If you edit the post to include relevant html, this would be easier to help with.

Try with cssSelector:

Inspect the login button. Within the inspector, right click the element and copy the CSS Selector.

driver.findElement(By.cssSelector("copypasta")).click();

Try to locate by xpath using several different methods listed in this useful cheat sheet.

For example, if the inner html text for your button is 'Login':

driver.findElement(By.xpath("//button[contains(text(), 'Login']")).click();

There are many different ways to do this, so looking at your html will help people help you, I think.



回答2:

This is an issue caused by the Firefox version in use, 49.0.2. It's a weird case and unclear why login functionality is being imparted by the browser version in use;however, resolving the issue requires a downgrade to version 46.0. This fixed the problem.