Search code examples
javaseleniumselenium-webdriveriodropdown

Getting value from file and selecting the same value from dropdown using selenium webdriver


I am facing this issue that i am trying to read from excel file and selecting the corresponding value from the dropdown on screen. Using the code:

cell = sheet.getRow(i).getCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
WebElement dr = driver.findElement(By.xpath("//*[@id =\"select2chosen-3\"]"));
dr.click();
driver.findElement(By.xpath("//li/div[contains(.,cell.getStringCellValue())]")).click();

Getting this error from eclipse:

org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //li/div[contains(.,cell.getStringCellValue())] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li/div[contains(.,cell.getStringCellValue())]' is not a valid XPath expression.


Solution

  • Try with following code,

    driver.findElement(By.xpath("//li/div[contains(.,'"+cell.getStringCellValue().trim()+"')]")).click();