Reputation: 11
I have a HTML code. The thing is that the option names contain lots of spaces until the tags are closed.
<SELECT NAME="auth" id="auth" Size=1 onChange="updatePageState()">
<OPTION value="0">Open System
</option>
<OPTION value="1">WEP
</option>
<OPTION value="2">WPA
</option>
<OPTION value="3">WPA2
</option>
I want to check that some option is selected in the dropdown box by using selenium in java. This is my code:
try {
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.textToBePresentInElementValue(((By.xpath("//select [@id='auth']/option['" + Authen + "']"))), Authen));
System.out.println("Authentification is correct");
} catch (Exception x) {
System.out.println("Authentification is incorrect");
x.printStackTrace();
}
where "Authen" is a variable read from a file which corresponds to the options in the dropdown box.
I get the following error message:
org.openqa.selenium.TimeoutException: Timed out after 20 seconds waiting for text ('WPA2') to be the value of element located by By.xpath: //select [@id='auth']/option['WPA2']
Any help on how to check if that string contains partial text? I cannot use the method .contains because it's of type boolean and .textToBePresentInElementValue needs to have the second attribute as type String.
Upvotes: 1
Views: 5848
Reputation: 402
You can check each option through this:
WebDriver driver;
ArrayList<String> valuesToCheck;
Select selectBox = new Select(driver.findElement(By.name("auth")));
List<WebElement> boxOptions = selectBox.getOptions();
int i = 0;
ArrayList<boolean> isPresent = new ArrayList<boolean>();
for(String val : valuesToCheck)
{
for(WebElement option : boxOptions)
{
selectBox.selectByIndex(i);
if(selectBox.getFirstSelectedOption().getText().equals(valueToCheck.get(i)))
{
isPresent.get(0) = true;
}
}
if(isPresent.get(0)!=true)
{
isPresent.get(0)=false;
}
}
this would return an array of booleans that check if the values you were looking for were present inside of the select statement.
its a little bit of a workaround, but i think it would do the job.
Also, i couldn't find documentation so this is coming out of pure opinion and could potentially not be fact, but doesn't the value attribute supposed to specify the text? that could be your current problem.
Upvotes: 0
Reputation: 16201
Use the xpath containts()
function to find the element. Such as
//select[@id='auth']/option[contains(text(),'Open System')]
Upvotes: 0
Reputation: 1182
Maybe it is easier to select the webelement with a less complex @FindBy expression and then wrap the element in a Select which provides a method to get the first selected option. You then can do your comparison.
Take a look at:
https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/Select.html
Upvotes: 1