我在嘗試執行拖放操作時看到StateElementReferenceException
,請參閱下面的代碼片段。任何人都可以請幫忙解決這個適當的建議和解釋?Selenium中的StateElementReferenceException
@Test(priority=4)
public void addUserBySearch() {
driver.findElement(By.xpath(OR.getProperty("btnUserGroup"))).click();
driver.findElement(By.xpath(OR.getProperty("btnCreateUG"))).click();
driver.findElement(By.xpath(OR.getProperty("textUGName"))).sendKeys("UserGroup:" + Utils.randGen());
driver.findElement(By.xpath(OR.getProperty("userSearchField"))).sendKeys("testuser2", Keys.ENTER);
source = driver.findElement(By.xpath(OR.getProperty("searchedUserSource")));
destination = driver.findElement(By.xpath(OR.getProperty("userDestination")));
waitUntilElementVisible(30, source);
dragAndDrop(source, destination);
driver.findElement(By.xpath(OR.getProperty("btnScheduleNow"))).click();
}
public void waitUntilElementVisible(int seconds, WebElement element) {
WebDriverWait explicitWait = new WebDriverWait(driver, seconds);
explicitWait.until(ExpectedConditions.visibilityOf(element));
}
public void dragAndDrop(WebElement sourceElement, WebElement destinationElement){
try {
if (sourceElement.isDisplayed() && destinationElement.isDisplayed()) {
Actions action = new Actions(driver);
action.dragAndDrop(sourceElement, destinationElement).build().perform();
}
else {
System.out.println("Element not found to drag and drop");
}
}
catch (StaleElementReferenceException exc) {
exc.printStackTrace();
}
catch (NoSuchElementException exc) {
exc.printStackTrace();
}
}
btnUserGroup = //*[text()='User Groups']
btnCreateUG = //*[@id='stage']/div/div[2]/div/div/div[2]/div[3]/a
textUGName = //input[@id='user_group_name']
btnScheduleNow = //*[text()='Schedule Now']
userDestination = //*[@class='ConnectedList ConnectedListAdded']/div[2]/ul
userSearchField = //div[@class='ConnectedListConnectedListSelect']/div[2]/div[1]/ul/li[1]/input
searchedUserSource = //div[@class='ConnectedList ConnectedListSelect']/div[2]/ul/li/span[5]
例外:
org.openqa.selenium.StaleElementReferenceException:元素在緩存中沒有發現 - 也許是頁面發生了變化,因爲它是擡頭 命令持續時間或超時:10.28秒
什麼元素是給你的例外呢? – Cathal
在com.suite.regression.RegressionSuiteBase.waitUntilElementVisible(RegressionSuiteBase.java:26) \t在com.suite.regression.UserGroup.addUserBySearch(UserGroup.java:60)我想 「源」 元素 –
在行waitUntilElementVisible(30,資源); addUserBySearch()方法和explicitWait方法。直到(ExpectedConditions.visibilityOf(元件)); waitUntilElementVisible(int seconds,WebElement元素)方法 –