我所試圖做的是從這個例子網站選擇多個選項按住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選擇所有選項。
歡迎來到SO。我不能完全理解你的問題。你的代碼中哪裏找到錯誤或問題? – nbayly
對不起,更新了這個問題。我想要做的是隻要在Excel中有一個值,它將通過excel和網站中的選項循環,並繼續使用CTRL的選擇所有選項,其文本值位於excel – Ami