【发布时间】:2020-09-02 13:50:12
【问题描述】:
我正在尝试创建一个新类,该类将包含用于我们的 Selenium 测试的所有变量。以下是课程:
类:变量Repo:
package src;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class variablesRepo {
// public static final WebElement ChkBox = null;
static WebDriver driver = new ChromeDriver();
static WebElement LoginButton = driver.findElement(By.id("btnLogin"));
}
类 NewTest.Java:
package src;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeSuite;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.annotations.AfterSuite;
public class NewTest {
WebDriver Driver = variablesRepo.driver;
@Test
public void TestElement() {
WebElement LoginButton = variablesRepo.LoginButton;
assert LoginButton.isDisplayed();
}
@BeforeSuite
public void beforeSuite() {
Driver.get("https://opensource-demo.orangehrmlive.com/");
}
@AfterSuite
public void afterSuite() {
Driver.close();
}
}
当我尝试运行测试时,浏览器打开,页面也打开,因此脚本能够从 veriableRepo 中检索变量。但是当涉及到 web 元素时,脚本会失败。看起来它正在尝试在 Repo 文件中定位 webelements。我收到以下错误:
原因:org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"#btnLogin"}
我们如何解决这个问题并从 Repo 类中导入变量?
更新:我得到的错误堆栈是:
[RemoteTestNG] 检测到 TestNG 版本 7.3.0 正在启动 ChromeDriver 85.0.4183.87 (cd6713ebf92fa1cacc0f1a598df280093af0c5d7-refs/branch-heads/4183@{#1689}) 在端口 25606 上只允许本地连接。请参见 https://chromedriver.chromium.org/security-considerations 为 有关保持 ChromeDriver 安全的建议。 ChromeDriver 已启动 成功地。 2020 年 9 月 2 日 11:13:42 org.openqa.selenium.remote.ProtocolHandshake createSession 信息: 检测到的方言:W3C org.testng.TestNGException:无法实例化 类 src.NewTest 在 org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:30) 在 org.testng.internal.InstanceCreator.instantiateUsingDefaultConstructor(InstanceCreator.java:193) 在 org.testng.internal.InstanceCreator.createInstanceUsingObjectFactory(InstanceCreator.java:113) 在 org.testng.internal.InstanceCreator.createInstance(InstanceCreator.java:79) 在 org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:109) 在 org.testng.internal.ClassImpl.getInstances(ClassImpl.java:167) 在 org.testng.TestClass.getInstances(TestClass.java:102) 在 org.testng.TestClass.initTestClassesAndInstances(TestClass.java:82) 在 org.testng.TestClass.init(TestClass.java:74) 在 org.testng.TestClass.(TestClass.java:39) 在 org.testng.TestRunner.initMethods(TestRunner.java:457) 在 org.testng.TestRunner.init(TestRunner.java:336) 在 org.testng.TestRunner.init(TestRunner.java:289) 在 org.testng.TestRunner.(TestRunner.java:220) 在 org.testng.remote.support.RemoteTestNG6_12$1.newTestRunner(RemoteTestNG6_12.java:33) 在 org.testng.remote.support.RemoteTestNG6_12$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_12.java:66) 在 org.testng.ITestRunnerFactory.newTestRunner(ITestRunnerFactory.java:55) 在 org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:676) 在 org.testng.SuiteRunner.init(SuiteRunner.java:178) 在 org.testng.SuiteRunner.(SuiteRunner.java:112) 在 org.testng.TestNG.createSuiteRunner(TestNG.java:1306) 在 org.testng.TestNG.createSuiteRunners(TestNG.java:1282) 在 org.testng.TestNG.runSuitesLocally(TestNG.java:1131) 在 org.testng.TestNG.runSuites(TestNG.java:1069) 在 org.testng.TestNG.run(TestNG.java:1037) 在 org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115) 在 org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) 在 org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77) 引起 通过:java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:23) ... 27 更多原因:java.lang.ExceptionInInitializerError at src.NewTest.(NewTest.java:13) ... 32 更多原因: org.openqa.selenium.NoSuchElementException:没有这样的元素:无法 定位元素:{"method":"css 选择器","selector":"#btnLogin"}
(会话信息:chrome=85.0.4183.83)有关此错误的文档, 请拜访: https://www.seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:'3.141.59',修订:'e82be7d358',时间: '2018-11-14T08:25:48' 系统信息:主机:'DML-02213',ip:'10.9.9.5', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_261' 驱动信息: org.openqa.selenium.chrome.ChromeDriver 功能 {acceptInsecureCerts: false, browserName: chrome, browserVersion: 85.0.4183.83,铬:{chromedriverVersion:85.0.4183.87(cd6713ebf92fa ...,userDataDir:C:\ Users \ nhassan \ AppData \ Lo ...}, goog:chromeOptions: {debuggerAddress: localhost:62831}, javascriptEnabled:真,networkConnectionEnabled:假, pageLoadStrategy:正常,平台:WINDOWS,平台名称:WINDOWS, 代理:代理(),setWindowRect:真,strictFileInteractability:假, 超时:{隐式:0,pageLoad:300000,脚本:30000}, unhandledPromptBehavior:解雇并通知, webauthn:virtualAuthenticators: true} 会话 ID: 6793977a08f3b6eefb43d17e34597557 *** 元素信息:sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 处的 {Using=id, value=btnLogin} 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知来源) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122) 在 org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49) 在 org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158) 在 org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323) 在 org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:372) 在 org.openqa.selenium.By$ById.findElement(By.java:188) 在 org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315) 在 src.variablesRepo.(variablesRepo.java:12) ... 33 更多
【问题讨论】:
-
您能发布完整的错误堆栈消息吗?哪一行代码导致了这个错误? repo 代码还是 NewTest 文件?
-
@Sureshmani 添加的错误堆栈 ID。感谢您的检查。
-
出现此错误时元素可能未加载。尝试添加一些等待时间。另一种可能性是元素可能隐藏在 iframe 后面。检查是否存在任何 iframe。
-
@Sureshmani 不,该元素不在任何 iFrame 后面。当我在本地调用元素时它工作正常,即 WebElement LoginButton = Driver.findElement(By.id("btnLogin"));断言 LoginButton.isDisplayed();
-
Variablerepo - 是静态导入 NewTest 的吗?我想这是初始化局部变量的问题。尝试使用非静态方法,例如创建类的新对象 - Variablerepo。
标签: java selenium selenium-webdriver testng