我正在尋找一些關於如何選擇WebElement的建議。 當我點擊一個按鈕創建一個新的'頁面'時,給定的值是動態的,因爲它是完全隨機的而不是順序的。我希望它會遵循如下內容:頁面1 = 0,頁面2 = 1,頁面3 = 2等。動態創建值時使用的最佳方法?
默認情況下,我以頁面開頭,其值爲c3
。雖然當我觸發一個按鈕來創建新頁面的值如下所示:
<select class="j-currentPage f-feature-A" name="currentPage"> <option value="c3">New Page</option> <option value="c277">New Page 2</option> <option value="c383">New Page 3</option> <option selected="" value="c461">New Page 4</option> </select>
所以,當我嘗試使用以下命令:
List<WebElement> option = chrome.findElements(By.tagName("option"));
option.get(0).click();
option.get(1).click();
option.get(0).click();
這顯然未能爲值是C277,C346等而且無法知道新課程中的價值觀,因爲它們總是會有所不同。
stacktrace拋出一個StaleElementReferenceException
,因爲它不在DOM中。
如何在數值完全隨機時選擇每個頁面?
我正在使用Webdriver和JUnit4。
編輯
測試方法:創造一些新的網頁。
@Test
public void createANewPage(){
WebDriverWait wait = new WebDriverWait(chrome, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.j-currentPage.f-feature-A[name=\"currentPage\"]")));
WebElement newPage = chrome.findElement(By.cssSelector("span.j-addViewBtn.ss-layers"));
newPage.click();
List<WebElement> option = chrome.findElements(By.tagName("option"));
option.get(1).click();
option.get(0).click();
}
再後來幾個測試中,我想再打創建的網頁
@Test
public void goToPage(){
new WebDriverWait(chrome, 60).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("select.j-currentPage.f-feature-A[name=\"currentPage\"]")));
List<WebElement> option = chrome.findElements(By.tagName("option"));
option.get(0).click();
option.get(1).click();
option.get(0).click();
}
我在第三option.get(0).click();
點收到org.openqa.selenium.StaleElementReferenceException
但是我感覺好像由於這些值是動態創建的,因此我正在接近這種錯誤方式<option value="c3">New Page</option>
<option value="c207">New Page 2</option>
<option value="c288">New Page 3</option>
<option selected="" value="c366">New Page 4</option>
這裏有一個問題。你想選擇任何一個選項嗎?或者一個特定的? –
我想有能力選擇我想要的網頁。因此,我想在稍後的測試中在頁面之間切換一個特定的頁面。 –
我沒有理解一些觀點。一旦你選擇了一個選項,它會轉發到其他頁面,你問的是「我怎樣才能選擇每一頁」,所以你想要的東西不清楚。你可以請更新你的問題一些更多的細節。 –