2017-04-02 85 views
0

这是我在C#中的代码我按下删除按钮并打开一个弹出式窗口,在那里我选择要删除的删除数量,然后再次按删除它将删除。 也许是更正确的问我怎么办,而这个删除元素C#硒web驱动程序如何做while循环元素

PropertiesCollections.driver.FindElement (By.LinkText ("Delete")). Click(); 

只要出现真正的,它会执行删除操作步骤,如果不继续测试

PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click(); 
new SelectElement(PropertiesCollections.driver.FindElement(By.Id("remove_shares"))).SelectByText("1"); 
PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click(); 

我想打一个循环,如果删除按钮出现,它会做删除的所有步骤,如果不继续为其他测试


IM尝试使用此代码

var links = PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click(); 
      while (links = true) 
       { 
       PropertiesCollections.driver.FindElement(By.LinkText("Delete")).Click(); 
       PropertiesCollections.driver.FindElement(By.Id("remove_shares")); 
       PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click(); 
      } 

,但我得到的错误 错误1无法分配空隙的隐式类型的局部变量

回答

1

的第一行代码被分配的.Click()给变量回归links,但.Click()返回void(无)。

你想要做的逻辑是:看到

  1. 检查的删除链接存在
  2. 如果是这样,单击它
  3. 做的东西
  4. 重复1-3

IReadOnlyCollection<IWebElement> links = PropertiesCollections.driver.FindElements(By.LinkText("Delete")); // gets a collection of elements with Delete as a link 
while (links.Any()) // if the collection is not empty, this evaluates to `true` 
{ 
    links.ElementAt(0).Click(); // click the first (and probably only?) element 
    // do stuff 
    PropertiesCollections.driver.FindElement(By.Id("remove_shares")); 
    PropertiesCollections.driver.FindElement(By.XPath("(//button[@type='button'])[2]")).Click(); 
    // get the Delete links again so we can return to the start of the `while` and see if it's still not empty 
    links = PropertiesCollections.driver.FindElements(By.LinkText("Delete")); 
} 
+0

非常感谢您的帮助,这有助于大大解决我的问题, 但我有一个问题,我不明白这一行> Links.ElementAt(0).Click(); //单击第一个(也许只有?)元素 //做东西 –

+0

它只是单击集合中的第一个元素。您可以查看'.ElementAt()'的文档以获取更多详细信息。 – JeffC

+0

如果您发现此(或任何)答案有帮助,请立即加入。如果这回答了您的问题,请将其标记为已接受的答案。谢谢! – JeffC