简体   繁体   English

当for循环为空时,元素不可见

[英]Element not visible when for loop is empty

I have a for-loop that clicks around a website and testing if the links are working. 我有一个for循环,可在网站上单击并测试链接是否有效。

But when I enter a page that doesn't have links the test obvious fails. 但是,当我进入一个没有链接的页面时,测试显然失败了。 But i cant figure out how to stop the test instead of failing it. 但是我不知道如何停止测试而不是失败。

This is my really simple for loop 这是我真正简单的for循环

public void TestT2Links()
{
    int count = LinkElements.Count;
    for (int i = 3; i < count; i++)
    {
        PropertiesCollection.driver.FindElements(By.TagName("a"))[i].Click();
    }
}

Error Log from VS VS的错误日志

Result Message: 结果消息:

OpenQA.Selenium.ElementNotVisibleException : element not visible
  (Session info: chrome=40.0.2214.93)
  (Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)

Result StackTrace: 结果StackTrace:

at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebElement.Click()

Any ideas on how i should stop it? 关于如何停止它的任何想法?

Add visibility check before you click: 单击之前添加可见性检查:

var e = PropertiesCollection.driver.FindElements(By.TagName("a"));
for (int i = 3; i < e.Count; i++) {
    if (e[i].Displayed) {
        e[i].Click();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM