What I am trying to do is select multiple options from this example site while holding CTRL
http://www.htmlcodetutorial.com/forms/_SELECT_MULTIPLE.html
The multiple selection values would come from excel though, means if the excel rows has values as mushroom, onions and olives each on different rows (within same column) it will select only those values one by one within the page. The Excel file looks like this :
And this is the code I got so far upto
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();
}
}
}
}
What i am trying to do is as long as there is a value in excel it will loop through excel and the options within the website and keep on selecting all the options using CTRL whose text values are located in excel
2 Answers 2
Following is the code:
Actions ac = new Actions(driver);
ac.keyDown(Keys.CONTROL);
for (Row r : sheet) {
Cell c = r.getCell(1);
String execelValue = c.getStringCellValue();
if (!execelValue.equalsIgnoreCase("Selection")) {
element = driver.findElement(By.xpath("//option[@value='" + execelValue + "']"));
element.click();
}
}
ac.keyUp(Keys.CONTROL);
This code will take value from excel, and based on that it will select the option from the list.
-
@Ami has this resolved your questionJeevan Bhushetty– Jeevan Bhushetty2016年04月14日 10:13:41 +00:00Commented Apr 14, 2016 at 10:13
@Ami - You are doing it in a complicated way I guess
Why don't you try to fetch all data you need like in the very simple example below?
public Object[][] signUpPage1FieldValidationData() throws Exception{
Object[][] retObjArr=getTestData("<your .xls file name","<your tab name>", "<fields which you wanted to fetch>");
return(retObjArr);
Explore related questions
See similar questions with these tags.