gpt4 book ai didi

java - 单击页面上已存在的元素时出现 TimeoutException

转载 作者:行者123 更新时间:2023-12-01 17:41:00 26 4
gpt4 key购买 nike

这是我的第一类 UserPage。在这里您可以看到从 BasePageObject 类调用 click 方法的方法 ClickNextPage()

public class UserPage extends BasePageObject {

private String buttonNextPage = "//button[contains(@aria-label, 'Next page')]";

public UserPage(WebDriver driver, Logger log) {
super(driver, log);
}

public UserPage ClickNextPage() {
click(By.xpath(buttonNextPage));
return this;
}

}

BasePageObject 类中有以下方法

方法 click() 调用方法 WaitForVisibilityOf 和 find()

调用waitFor方法的方法WaiforVisibility
public class BasePageObject {
protected WebDriver driver;
protected Logger log;
protected String iconXpath = ".//mat-icon[@aria-label='%s']";

public BasePageObject (WebDriver driver, Logger log){
this.driver = driver;
this.log = log;

}
protected void click(By locator) {
waitForVisibilityOf(locator, 10);
find(locator).click();
}
protected WebElement find(By locator){
return driver.findElement(locator);
}
/**
* Wait for specific ExpectedCondition for the given amount of time in seconds
*/
private void waitFor(ExpectedCondition<WebElement> condition, Integer timeOutInSeconds) {
timeOutInSeconds = timeOutInSeconds != null ? timeOutInSeconds : 30;
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
wait.until(condition);
}

/**
* Wait for given number of seconds for element with given locator to be visible
* on the page
*/
protected void waitForVisibilityOf(By locator, Integer... timeOutInSeconds) {
int attempts = 0;
while (attempts < 2) {
try {
waitFor(ExpectedConditions.visibilityOfElementLocated(locator),
(timeOutInSeconds.length > 0 ? timeOutInSeconds[0] : null));
break;
} catch (StaleElementReferenceException e) {
}
attempts++;
}
}

现在我在我的测试中使用这些方法
@org.testng.annotations.Test()
public void login() {

MainPage mainPage = new MainPage(driver,log);
mainPage.openPage();

UserPage userPage = mainPage.login(login, password);

userPage.ClickNextPage();

但我收到一个错误
TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //button[contains(@aria-label, 'Next page')]

at dtapi.pages.BasePageObject.waitFor(BasePageObject.java:120)
at dtapi.pages.BasePageObject.waitForVisibilityOf(BasePageObject.java:131)
at dtapi.pages.BasePageObject.click(BasePageObject.java:99)
at dtapi.pages.UserPage.ClickNextPage(UserPage.java:27)
at dtapiLoginTest.LoginPageTests.login(LoginPageTests.java:33)

我必须承认元素是可见的和存在的

更新

我做了一些更改,现在可以使用
public class UserPage extends BasePageObject {
private String buttonNextPage = "//button[@class='mat-paginator-navigation-next mat-icon-button mat-button-base']";

public UserPage(WebDriver driver, Logger log) {
super(driver, log);

}
public void ClickNextPage() {

WebElement butNexPage = find(By.xpath(buttonNextPage));

butNexPage.click();

}

}

但是当我使用这个 xpath 它不起作用
private String buttonNextPage = "//button[@aria-label='Next page']";

Error
NoSuchElementException
at dtapi.pages.BasePageObject.find(BasePageObject.java:95)
at dtapi.pages.UserPage.ClickNextPage(UserPage.java:30)
at dtapiLoginTest.LoginPageTests.login(LoginPageTests.java:33)

我想使用这个 xpath,因为我可以在需要时更改方法中的 aria-label

xpath

最佳答案

您正在尝试单击元素,因此如果您更新解决方案
到 elementToBeClickable 而不是 visibilityOfElementLocated。 visibilityOfElementLocated 不保证可点击性。

waitFor(ExpectedConditions.elementToBeClickable(locator),
(timeOutInSeconds.length > 0 ? timeOutInSeconds[0] : null));

此外,如果它不起作用,请尝试从 10 to 20 增加等待时间.

关于java - 单击页面上已存在的元素时出现 TimeoutException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948887/

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