I would like to use a function to show on the screen the current value of a dropdown list, because the Selenium code isn't pretty.
The following works perfectly in my main script:
new Select(driver.findElement(By.id("secId:mainBody:vboxlist:s_m9_aa2::content"))).selectByVisibleText(contract.getCreditype());
But using the function, it doesn't work:
writeScreen("Description : ", "secId:mainBody:vboxlist:s_m9_aa2::content");
public void writeScreen(String description, String identifiant) {
String xpath="//*[@id=\\\""+identifiant+"\\\"]";
Select select = new Select(driver.findElement(By.xpath(xpath)));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(description+defaultItem);
I got the following error:
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"]' is not a valid XPath expression.
When I checking the XPATH, it's exactly the same as in my first working example.