selenium webdriver - Selenium2: c#: while循环的帮助

本文关键字:循环 帮助 while Selenium2 selenium webdriver | 更新日期: 2023-09-27 17:52:11

我需要帮助的while循环,下面有一个页面的代码,我已经做到了:

<div class="list">
<span class="destination">
<a tabindex="20" href="/flights-to/uk/cheap-flights-to-united-kingdom.html">United&nbsp;Kingdom</a>
</span>
<span class="destination">
<a tabindex="21" href="/flights-to/es/cheap-flights-to-spain.html">Spain</a>
</span>
<span class="destination">
<a tabindex="22" href="/flights-to/us/cheap-flights-to-united-states.html">United&nbsp;States</a>
</span>
<span class="destination">
<span class="destination">
<span class="destination">
<span class="destination">
<span class="destination">
<span class="destination">
<span class="destination">
<a tabindex="29" href="/flights-to/belf/cheap-flights-to-belfast.html">Belfast</a>
</span>
</div>

我必须准备脚本,将在每个链接上点击,并检查是否有404错误等。

我可以在Selenium IDE中做到这一点,但我不知道我应该如何从c#和Selenium2开始。也许有人有什么建议?

selenium webdriver - Selenium2: c#: while循环的帮助

这样应该可以。

string url = "testpageurl";
IWebdriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl(url);
var links = driver.FindElements(By.TagName("a"));
for(int i=0; i<links.Count; i++ ) 
{
driver.Navigate().GoToUrl(url);
driver.FindElements(By.TagName("a"))[i].Click();
Assert.IsFalse(driver.FindElement(By.TagName("body")).Text.Contains("404"));
}