我要做的是在按住CTRL
的同时从此示例网站中选择多个选项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和网站中的选项,并继续使用CTRL选择所有选项,其文本值位于excel中
答案 0 :(得分: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();
答案 1 :(得分: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();
惊人的家伙。非常感谢。