gpt4 book ai didi

eclipse - Webdriver可以登录Chrome,不能登录Firefox : 'Unable to find owning document' error

转载 作者:行者123 更新时间:2023-12-04 03:16:45 28 4
gpt4 key购买 nike

我在 java testNG 项目中有 2 个属于同一个包的类,并且我在 A 类中声明了“webdriver driver”公共(public)静态。在该类中,启动了 chrome,打开了 url,输入了用户名和密码并单击了登录按钮。使用 @BeforeClass 注释工作正常。

我将相同的代码复制到 B 类中,并将浏览器实例更改为 firefox,同时仍将“webdriver 驱动程序”声明为公共(public)静态。 Firefox 启动,URL 打开,输入用户名和密码,但登录按钮没有点击或提交。测试失败并出现以下错误:

org.openqa.selenium.JavascriptException:错误:无法找到拥有的文档。

我从来没有遇到过这个错误,也不知道“拥有文档”指的是什么。我怀疑它与一个或两个类的访问级别有关。以下是 2 类的摘录。我错过了什么吗?

*

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

最佳答案

编辑:看到这个非常相关的question and answer

如果您编辑帖子以包含相关的 html,这将更容易提供帮助。

尝试使用 cssSelector:

检查登录按钮。在检查器中,右键单击元素并复制 CSS 选择器。

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

尝试使用此有用的 cheat sheet. 中列出的几种不同方法通过 xpath 进行定位

例如,如果按钮的内部 html 文本是“登录”:
driver.findElement(By.xpath("//button[contains(text(), 'Login']")).click();

有很多不同的方法可以做到这一点,所以我认为查看你的 html 会帮助人们帮助你。

关于eclipse - Webdriver可以登录Chrome,不能登录Firefox : 'Unable to find owning document' error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40386805/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com