2016-03-08 82 views
0

我所試圖做的是從這個例子網站選擇多個選項按住CTRL選擇多個選項,並選擇值會從Excel

http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html

多重選擇值將來自Excel中,雖然,手段如果excel行的值分別爲不同行(同一列中)的蘑菇,洋蔥和橄欖,則只會在頁面內逐一選擇這些值。 Excel文件看起來是這樣的:

http://i.imgur.com/tcEB2wX.png

這是我到目前爲止的代碼高達

package mineP; 

import java.io.File; 
import java.io.FileInputStream; 
import java.util.List; 

import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class Various { 

    public static void main(String[] args) throws Exception{ 

       File src = new File("C:\\Users\\Documents\\myP2.xlsx"); 

       // Load file 
       FileInputStream fis = new FileInputStream(src); 

       // Load WB 
       XSSFWorkbook wb = new XSSFWorkbook(fis); 

       // Load Sheet 

       XSSFSheet sh1 = wb.getSheetAt(0); 



     String chromePath = "C:\\Users\\chromedriver.exe"; 
     System.setProperty("webdriver.chrome.driver", chromePath); 



     WebDriver driver = new ChromeDriver(); 
     driver.manage().window().maximize(); 

     driver.get("http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html"); 


     WebElement sel = driver.findElement(By.xpath("//select[@name='toppings']")); 

     List<WebElement> alloptions = sel.findElements(By.xpath("//select[@name='toppings']//option")); 

     for (WebElement option: alloptions) { 

      String optTxt = option.getText(); 

      //System.out.println(optTxt); 

      if (optTxt.contains(sh1.getRow(3).getCell(1).getStringCellValue())){ 

       option.click(); 
      } 

     } 



    } 
} 

什麼,我試圖做的,只要有在Excel中它的值是將通過excel和網站中的選項進行循環,並繼續使用其文本值位於excel中的CTRL選擇所有選項。

+0

歡迎來到SO。我不能完全理解你的問題。你的代碼中哪裏找到錯誤或問題? – nbayly

+0

對不起,更新了這個問題。我想要做的是隻要在Excel中有一個值,它將通過excel和網站中的選項循環,並繼續使用CTRL的選擇所有選項,其文本值位於excel – Ami

回答

0

就像真實用戶會做的那樣,您可以按住Ctrl鍵並單擊每個元素:

WebDriver driver = new FirefoxDriver(); 
driver.get("http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html"); 
List<WebElement> options = driver.findElements(By.xpath("//select[@name='toppings']//option")); 

// Values to select 
List<String> values = Arrays.asList("onions", "olives"); 

// Select all the options 
Actions act = new Actions(driver); 
act.keyDown(Keys.CONTROL); 
for (WebElement option: options){ 
    if(values.contains(option.getText())) { 
     act.click(option); 
    } 
} 
act.keyUp(Keys.CONTROL); 
act.perform(); 

driver.quit(); 

,並從工作簿加載數據到ListArray:

List<String> values = new ArrayList<String>(); 

FileInputStream stream = new FileInputStream("C:\\file.xlsx"); 
XSSFWorkbook workbook = new XSSFWorkbook(stream); 
XSSFSheet worksheet = workbook.getSheetAt(0); 
Iterator<Row> rows = worksheet.rowIterator(); 
while (rows.hasNext()) { 
    Row row = rows.next(); 
    if(row.getRowNum() > 0) {        // skip first row 
     values.add(row.getCell(1).getStringCellValue()); // add the second cell 
    } 
} 
stream.close(); 
+0

,但值不是來自Excel中。您使用的代碼是由我提供的靜態值。我希望來自excel的值和循環我希望繼續選擇所有選項,只要在Excel中有一個值。並且值可能會隨着時間在Excel中改變,所以選擇也​​會改變。謝謝。 – Ami

+0

我添加了一個例子來將數據從工作簿加載到ListArray。 –

+0

非常感謝。你會友好地縫合兩個代碼,以顯示它如何一起工作嗎?再次感謝。 – Ami

0

謝謝你這麼多@florentbr它的工作:d。結合起來,沒有任何問題的工作。這裏是組合的代碼,如果有人需要它 -

// =========The SpreadSheet========= 

    File src = new File("C:\\file.xlsx"); 

    // Load file 
    FileInputStream fis = new FileInputStream(src); 

    // Load WB 
    XSSFWorkbook wb = new XSSFWorkbook(fis); 

    // Load Sheet 
    XSSFSheet sh1 = wb.getSheetAt(0); 

    // ==========The Browser=========== 

    String chromePath = "C:\\chromedriver.exe"; 
    System.setProperty("webdriver.chrome.driver", chromePath); 

    WebDriver driver = new ChromeDriver(); 
    driver.manage().window().maximize(); 

    driver.get("http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html"); 

    List<WebElement> options = driver.findElements(By.xpath("//select[@name='toppings']//option")); //Find the options within this element within the page 

    List<String> values = new ArrayList<String>(); //values from excel will be stored within this array 

    Iterator<Row> rows = sh1.rowIterator(); 


    while (rows.hasNext()) 
    { 

     Row row = rows.next(); 

     if (row.getRowNum() > 0) 
     { 

      values.add(row.getCell(1).getStringCellValue()); 

     } 
    } 

    System.out.println(values); 

    Actions act= new Actions(driver); 

    act.keyDown(Keys.CONTROL); 

    for (WebElement option:options) 
    { 

     if (values.contains(option.getText())) { 
      act.click(option); 
     } 
    } 

    act.keyUp(Keys.CONTROL); 
    act.perform(); 

    Thread.sleep(2000); 



    fis.close(); 

令人驚歎的傢伙。非常感謝。