2015-03-02 110 views
0

我想做一個連接到網站的程序,並點擊它上面的所有按鈕。問題是這些按鈕是「相等的」,我相同的意思是在它們中有相同的文本,相同的鏈接和相同的CSS。硒處理等元素

考慮以下結構:

<content> 
<line1> 
    <block1> some text </block1> 
    <button> Start! </button> 
<line2> 
    <block2> some different text </block2> 
    <button> Start! </button> 
<line3> 
    <block3> some text different than 1 and 2 </block3> 
    <button> Start! </button> 

我如何才能讓一個函數來單擊所有的按鈕?我已經嘗試用xpath找到它們,但是一旦我將它們放在列表中,我不能單擊它們,因爲我無法設置正確的等待,因此沒有任何反應。

回答

0

將具有相同屬性的所有按鈕添加到列表中,並迭代列表。我試過它對我來說非常合適。

List buttons = driver.findElements(By.xpath("//button[text()='button text']")); 

     for(int i=0;i<=buttons.size();i++){ 

     ((WebElement) buttons.get(i)).click(); 

     } 

請告訴我,如果它爲你工作...

+0

是的,它的工作原理!非常感謝你!:-) – 2015-03-02 15:58:50

0

嘗試使用以下內容: 1)創建列表;將所有按鈕放在那裏,通過點擊每個元素(即按鈕)遍歷列表。

 List <WebElement> buttonList= 
driver.findElements(By.cssSelector("button")); 
     for(int i=0; i<buttonList.size(); i++) 
     { 
      buttonList.get(i).click(); 
     } 

希望這可以幫助你。

+0

它的工作原理!非常感謝!:-) – 2015-03-02 15:58:32