我試圖在登錄系統后立即單擊儀表板上資料表中的第一個元素,但不知何故僅在登錄后顯示測驗用例已通過,并且儀表板上資料表上的記錄仍未單擊。
這是我的實作
另一個檔案主頁上的 Xpath 用于資料表中的記錄

public final List<WebElement> listStudentsWhoMetGoals =
driver.findElements(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a"));
單擊表中第一條記錄的代碼
public void clickFirstStudent()
{
if(listStudentsWhoMetGoals.size() > 0)
{
ExplicitWaitFactory.performExplicitWaitForList
(WaitStrategy.CLICKABLE, listStudentsWhoMetGoals);
listStudentsWhoMetGoals.get(0).click();
return new StudentTabPage();
}
else
{
DriverManager.getWebDriver().quit();
}
}
測驗用例執行
LoginPage loginPage = new LoginPage();
loginPage.clickLoginButton().setUserName("admin").setPassword("xyzabc").clickSubmit().
clickFirstStudent();
顯式工廠代碼
public class ExplicitWaitFactory
{
public static WebElement performExplicitWait(WaitStrategy wait, By by)
{
WebElement element = null;
if(wait == WaitStrategy.CLICKABLE)
{
element = new WebDriverWait(DriverManager.getWebDriver(), FrameworkConstants.getExplicitwait())
.until(ExpectedConditions.elementToBeClickable(by));
}
else if(wait == WaitStrategy.VISIBLE)
{
element = new WebDriverWait(DriverManager.getWebDriver(), FrameworkConstants.getExplicitwait())
.until(ExpectedConditions.visibilityOfElementLocated(by));
}
else if(wait == WaitStrategy.PRESENCE)
{
element = new WebDriverWait(DriverManager.getWebDriver(), FrameworkConstants.getExplicitwait())
.until(ExpectedConditions.presenceOfElementLocated(by));
}
else if(wait == WaitStrategy.NONE)
{
element = DriverManager.getWebDriver().findElement(by);
}
return element;
}
測驗用例在登錄后立即通過,甚至沒有呼叫該方法來單擊記錄,但是,如果我在儀表板上選擇另一個元素,它會被單擊并發生操作,但是在這種情況下尤其存在問題,我無法從表中選擇第一條記錄
And if I select hardcoded first value from the table I am able to click it but that is not my motive, I wish to have all the values in a list and then click the first one.
uj5u.com熱心網友回復:
- 您的 xpath 看起來不可靠,但可能有效。
- 理想情況下,我會使用顯式等待。
所以而不是
public final List<WebElement> listStudentsWhoMetGoals =
driver.findElements(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a"));
嘗試這個
public final List<WebElement> listStudentsWhoMetGoals = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a")));
findElement用途implicit wait
通過隱式等待,WebDriver 在嘗試查找任何元素時會在一段時間內輪詢 DOM。當網頁上的某些元素無法立即使用并且需要一些時間來加載時,這會很有用。
請包括以下行
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
如果您想依賴findElement. 但我會和你爭論為什么?為什么不顯式等待。
警告:
不要混合隱式和顯式等待。這樣做可能會導致不可預測的等待時間。例如,設定 10 秒的隱式等待和 15 秒的顯式等待可能會導致在 20 秒后發生超時。
還請記住,有4 種方法可以在 Java-Selenium 系結中單擊,而且我看到您希望單擊第一個元素。
代碼試用1:
Thread.sleep(5);
driver.findElement(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a")).click();
代碼試用2:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a"))).click();
代碼試用3:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
代碼試用4:
Thread.sleep(5);
WebElement button = driver.findElement(By.xpath("(//*[@id='table-propeller'])[4]//td[@data-title='Top Student']//a"));
new Actions(driver).moveToElement(button).click().build().perform();
uj5u.com熱心網友回復:
問題已經有了很好的答案,但我只是在更正您的ExplicitWaitFactory代碼。
public class ExplicitWaitFactory
{
public static WebElement performExplicitWait(WaitStrategy wait, By by)
{
WebElement element = null;
if(wait == WaitStrategy.CLICKABLE)
{
element = new WebDriverWait(DriverManager.getWebDriver(), FrameworkConstants.getExplicitwait())
.until(ExpectedConditions.elementToBeClickable(by));
}
}
}
方法引數是WaitStrategy,By但你正在傳遞List<WebElement>如下,
ExplicitWaitFactory.performExplicitWaitForList
(WaitStrategy.CLICKABLE, listStudentsWhoMetGoals);
對于無效的引數型別和更改以下內容以使其作業是不可接受的,
更新檢查:
ExplicitWaitFactory.performExplicitWaitForList
(WaitStrategy.CLICKABLE, listStudentsWhoMetGoals.get(0));
更新方法:
public class ExplicitWaitFactory
{
public static WebElement performExplicitWait(WaitStrategy wait, WebElement ele)
{
WebElement element = null;
if(wait == WaitStrategy.CLICKABLE)
{
element = new WebDriverWait(DriverManager.getWebDriver(), FrameworkConstants.getExplicitwait())
.until(ExpectedConditions.elementToBeClickable(ele));
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311627.html
標籤:java selenium selenium-webdriver ui-automation webautomation
