我正在使用Selenium 4,并尝试查找一些元素(用户名和密码凭据)"用户名:管理员密码:admin123 "使用相对定位器selenium 4新功能,但代码抛出org. openqa. selenium. NoSuchElementException:无法使用[unknown locator]定位元素,但当我调试代码来测试它时,它成功通过,找到元素并在控制台上打印值。有人有建议的解决方案吗?
org.openqa.selenium.NoSuchElementException: Cannot locate an element using \[unknown locator\]
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.4.0', revision: 'e5c75ed026a'
System info: host: 'DESKTOP-M0R33E6', ip: '192.168.1.4', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '13.0.2'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.By.findElement(By.java:124)
at org.openqa.selenium.remote.ElementLocation$ElementFinder$1.findElement(ElementLocation.java:136)
at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:80)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:365)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:357)
at OrangeHrm.Pages.LoginPage.getUsernameUsingRelativeLocator(LoginPage.java:29)
at OrangeHrm_Tests.Login.LoginTests.test_GetUserCredentials(LoginTests.java:10)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
at org.testng.internal.invokers.TestInvoker.invokeMethod(TestInvoker.java:673)
at org.testng.internal.invokers.TestInvoker.invokeTestMethod(TestInvoker.java:220)
at org.testng.internal.invokers.MethodRunner.runInSequence(MethodRunner.java:50)
at org.testng.internal.invokers.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:945)
at org.testng.internal.invokers.TestInvoker.invokeTestMethods(TestInvoker.java:193)
at org.testng.internal.invokers.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.invokers.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1507)
at org.testng.TestRunner.privateRun(TestRunner.java:808)
at org.testng.TestRunner.run(TestRunner.java:603)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:429)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:423)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:383)
at org.testng.SuiteRunner.run(SuiteRunner.java:326)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:95)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1249)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.runSuites(TestNG.java:1092)
at org.testng.TestNG.run(TestNG.java:1060)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
网址:https://opensource-demo.orangehrmlive.com/web/index.php/auth/login
LoginPage.java
package OrangeHrm.Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage {
private WebDriver driver;
private By loginPageTitle = By.tagName("h5");
WebElement usernameCredential;
WebElement passwordCredential;
public LoginPage(WebDriver driver){
this.driver = driver;
}
public String getUsernameUsingRelativeLocator(){
usernameCredential = driver.findElement(RelativeLocator.with(By.xpath("//div[contains(@class , 'oxd-sheet--rounded')]/p[1]"))
.below(loginPageTitle));
System.out.println(usernameCredential.getText());
return usernameCredential.getText();
}
public String getPasswordUsingRelativeLocator(){
passwordCredential = driver.findElement(RelativeLocator.with(By.xpath("//div[contains(@class , 'oxd-sheet--rounded')]/p[2]"))
.below(username));
System.out.println(passwordCredential.getText());
return passwordCredential.getText();
}
}
OrangeHrmWebTestBase.java
package OrangeHrm_Tests.Base;
import OrangeHrm.Pages.LoginPage;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.time.Duration;
public class OrangeHrmWebTestBase {
private WebDriver driver;
protected LoginPage loginPageObj;
@BeforeClass
public void setUp(){
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://opensource-demo.orangehrmlive.com/");
loginPageObj = new LoginPage(driver);
}
@AfterClass
public void teatDown(){
driver.quit();
}
}
LoginTests.java
package OrangeHrm_Tests.Login;
import OrangeHrm_Tests.Base.OrangeHrmWebTestBase;
import org.testng.annotations.Test;
public class LoginTests extends OrangeHrmWebTestBase {
@Test
public void test_GetUserCredentials(){
loginPageObj.getUsernameUsingRelativeLocator();
loginPageObj.getPasswordUsingRelativeLocator();
}
}
1条答案
按热度按时间plicqrtu1#
在OrangeHRM网站中使用Below作为Relative Locator似乎非常接近。
但是,不要使用带有文本Username:的
<div>
管理员和密码:admin123作为相对定位器,您可以使用带有文本的<label>
作为***用户名***和*密码。代码块:
浏览器快照: