当前位置: 首页 > 知识库问答 >
问题:

Webdriver可以登录Chrome而不是Firefox:“找不到拥有的文档”错误

洪高阳
2023-03-14

我在java testNG项目中有两个属于同一个包的类,我在类a中声明了'html" target="_blank">webdriver driver'公共静态。在类中,chrome启动,打开url,输入用户名和密码,并单击登录按钮。使用@BeforeClass注释效果良好。

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

JavaScriptException:错误:找不到拥有的文档。

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

*

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个答案

都乐逸
2023-03-14

编辑:查看这个非常相关的问题和答案

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

尝试使用CSSSelector:

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

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

尝试使用本实用备忘单中列出的几种不同方法通过xpath进行定位。

例如,如果您的按钮的内部html文本是“登录”:

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

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

 类似资料:
  • 我正在尝试用Chrome运行硒测试。我正在使用C#。 看起来像是chromedriver。已找到exe,但它可以找到Chrome二进制文件。我设置了通往chrome的路径。自动搜索失败后显式执行。我甚至在最后用“chrome.exe”试过了。我总是得到同样的结果: 在以下位置找不到Chrome二进制文件: C:\用户\Vilem\AppData\本地\谷歌\Chrome\应用程序 仅供参考:我有一

  • 我使用Java ASM库编译了一个类(称为Test.class)。我决定尝试使用“java”命令运行该类,以确保已正确编译该类。 然而,当我这样做的时候,我得到了“找不到或加载主类测试”的消息。我觉得这很奇怪,因为类在当前工作目录内(我也尝试过将“.”作为类路径传递,但没有用)。然后我尝试用“javap”反汇编它,这很完美,这很奇怪,因为如果javap能找到类文件,那么java肯定也能找到吗? 这

  • 问题内容: 我正在自动执行一个调用CAPTCHA来验证登录名的网页,但是我注意到该页面仅在使用自动化测试代码时才请求此CAPTCHA,而当我手动执行时则不请求。我要求开发人员团队在质量检查环境中禁用它,但是通过安全代码是不可能的。 我需要知道是否有一种方法可以不对浏览器说我正在此页面上使用自动测试。 你能帮我吗? 问题答案: 不 ,没有任何方法可以隐瞒您正在运行自动化测试。 WebDriver界面

  • 在我的硒脚本中面对“Chrome不可达的错误”。几天前它工作得很好,但突然它抛出了错误。 Chrome浏览器版本:86.0.4240.111ChromeDrive版本:86.0.4240.22

  • 问题内容: 我正在使用Sphinx记录非Python项目。我想在每个子模块中分发文件夹,其中包含用于记录该模块文件的文件。然后,我想将这些文件吸收到主层次结构中,以创建整个设计的规范。 即: 我试图像这样在主文档toctree中包含文件: 但是,此错误消息导致: 警告:toctree包含对不存在的文档u’modules / module1 / docs / module1’的引用 是否无法以某种方

  • 问题内容: 我可以配置为将日志写在文件上,而不是在控制台上打印吗? 问题答案: 2013年更新- 围绕Node v0.2和v0.4编写;现在,围绕日志记录有更好的工具。我强烈推荐温斯顿 2013年末更新- 我们仍然使用winston,但现在有了记录器库,用于围绕自定义对象的记录和格式来包装功能。这是我们logger.js的示例 https://gist.github.com/rtgibbons/7