2016-03-08 81 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(); 

令人惊叹的家伙。非常感谢。