[英]Selenium webdriver - How to click the next button until Not able to find Next button in c#
我有一個響應式網站,其中包含36個按鈕。乍一看,我們只能看到12個按鈕,我需要驗證它們的文本值(我知道該怎么做)。但是問題是我需要單擊下一步按鈕,直到不可見。 那么我該如何在C#中使用硒呢?
另外,在單擊下一步時,我們需要將按鈕的文本存儲在列表或數組中,以便我可以將所有列表添加到新列表中並進行比較。
看下面我嘗試過的代碼
while (respRightNav.Displayed)
{
IList<IWebElement> sportsBtn = Driver.FindElements(By.CssSelector(".sports-buttons-responsive>ul li[class='sport-button']"));
foreach (var item in sportsBtn)
{
string btnText = item.Text.Replace(System.Environment.NewLine, "");
if (btnText == btnText.ToUpper())
{
Console.WriteLine("Sports Button : " + btnText + " is In Upper Case");
}
else
{
Assert.Fail("Sports Button Text is not Upper Case for : " + btnText);
}
}
respRightNav.Click();
if (respRightNav.Displayed.Equals(false))
{
break;
}
現在上面的工作正常了,但是當“下一個”按鈕不可用時,我仍然停留在上面,我仍然需要在foreach循環上運行以獲取文本(如果(respRightNav.Displayed.Equals(false)))。 意味着我需要運行Foreach循環
任何幫助,將不勝感激。
您可以使用while
循環檢查“ .Displayed”值:
IWebElement button;
while((button = driver.FindElementByXPath("//input[.='Next']")).Displayed) {
button.Click();
...
}
或在與可見元素匹配的單個調用中使用FindElementsByXPath
:
ReadOnlyCollection<IWebElement> buttons;
while((buttons = driver.FindElementsByXPath("//input[.='Next' and not(contains(@class,'none'))]")).Count > 0) {
buttons[0].Click();
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.