c#
  • selenium
  • xpath
  • webdriver
  • css-selectors
  • 2014-07-15 63 views 2 likes 
    2

    在Visual Studio中编写Selenium WebDriver的代码时,同一按钮的这两个代码只能正常工作一次。单击具有相同CssSelector或相同XPath的所有元素FindElements

    点击按钮通过CSS选择器:

    driver.FindElement(By.CssSelector(".follow-text")).Click(); 
    

    点击按钮与XPath:

    driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")).Click(); 
    

    直到这个正确的...


    但我想点击所有按钮不只是第一个,并且由于FindElements(复数)让我错误,我怎么能点击所有按钮与相同的代码?

    使用此得到错误:

    List<IWebElement> textfields = new List<IWebElement>(); 
    driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")).Click(); 
    driver.FindElement(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn'][3]")).Click(); 
    

    见捕获:

    enter image description here

    回答

    1

    您需要遍历FindElements结果,并在每个项目上调用.Click()

    var result = driver.FindElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")); 
    foreach (IWebElement element in result) 
    { 
        element.Click(); 
    } 
    

    仅供参考,您需要将XPath括在括号内以使您使用XPath索引的尝试代码工作:

    driver.FindElement(By.XPath("(//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn'])[3]")).Click(); 
    
    +0

    我在尝试,然后我现在链接一个捕获,谢谢 – Lion6

    0
    List <WebElement> list = driver.FindElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")); 
    

    然后遍历列表中包含的元素列表:

    int x = 0; 
    while (x < list.size()) { 
        WebElement element = list.get(x); 
        element.click(); 
    } 
    
    +0

    出现此错误,请参阅捕获请:http://postimg.org/image/6jhaj5ncf/ – Lion6

    +0

    我想说感谢您的时间来阅读和帮助 – Lion6

    0

    您应该使用类似的东西(注意findElements的S)

    List<WebElement> textfields = driver.findElements(By.XPath("//button[@class='user-actions-follow-button js-follow-btn follow-button btn small small-follow-btn']")); 
    

    ,然后用迭代for循环

    for(WebElement elem : textfields){ 
        elem.click(); 
    } 
    
    +0

    嗯...期待的错误;请看:http://postimg.org/image/j272r8m9p/ – Lion6

    +0

    编辑,只是遗忘了(); – singe3

    +0

    出现此错误,我做什么坏..? mm aix:http://s22.postimg.org/d38bhviyp/QUINTO.jpg谢谢 – Lion6

    相关问题