正常运行一段时间后,Selinium代码中断

时间:2019-04-11 18:26:51

标签: selenium

所以我有一个网页,在以下html代码中包含37个徽标,我需要在“ alt”属性内提取文本。

HTML代码如下:

<div class="logo__wrapper col-6 col-sm-4 col-md col-xl -gutter-bottom">
   <div data-lazy="true" data-src="https://www.talkdesk.com/app/themes/talkdesk-com/assets/images/logos/ibm.svg" data-alt="ibm" data-class="logo">
      <img src="https://www.talkdesk.com/app/themes/talkdesk-com/assets/images/logos/ibm.svg" alt="ibm" class="logo" data-load="lazy"></div>
</div>

我的C#如下:

 IReadOnlyCollection<IWebElement> customersPresent = driver.FindElements(By.XPath("//div[@class='logo__wrapper col-6 col-sm-4 col-md col-xl -gutter-bottom']"));              
 Assert.Greater(customersPresent.Count, 0);
 string[] customersArray = new string[customersPresent.Count];
 int conta = 0;
 foreach (IWebElement clientes in customersPresent)
 {
   customersArray[conta] = clientes.FindElement(By.TagName("div")).GetAttribute("data-alt");                
   conta++;
   TestContext.Progress.WriteLine("Logos Name Customers Page: " + customersArray[conta]+ "Conta" +conta);
            }

代码运行正常,但是在31次代码失败后,我收到以下消息:

消息:OpenQA.Selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“ method”:“标记名”,“ selector”:“ div”}

我已经选择不了,可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我会尝试以下方法。

 IReadOnlyCollection<IWebElement> customersPresent = driver.FindElements(By.XPath("//div[@class='logo__wrapper col-6 col-sm-4 col-md col-xl -gutter-bottom']/doc"));

现在直接为每个选择的集合获取“ data-alt”属性