0
votes
My HTML code is here:
    <fieldset>
<div class="clearfix">
<div class="clearfix">
<div class="clearfix">
<div class="clearfix">
<div class="qs-formfield-short qs-required">
<label for="stateCountry">State or Province</label>
<span class="k-widget k-dropdown k-header" style="" title="" unselectable="on" role="listbox" aria-haspopup="true" aria-expanded="false" tabindex="0" aria-owns="stateCountry_listbox" aria-disabled="false" aria-readonly="false" aria-busy="false">
<span class="k-dropdown-wrap k-state-default" unselectable="on">
<span class="k-input" unselectable="on">Please Select...</span>
<span class="k-select" unselectable="on">
<span class="k-icon k-i-arrow-s" unselectable="on">select</span>
</span>
</span>

My Code is here:

WebElement stateDropDown = driver.findElement( By.xpath("/html/body/form/div[3]/main/div/div/div/span/div/fieldset/div[4]/div[1]/span/span[1]")); List options = stateDropDown.findElements(By.xpath("/html/body/div[1]/div/ul/li[44]"));

    for(WebElement opt : options){
        if ("Texas".equals(opt.getText()));
        opt.click();
        System.out.println(opt);
1
Please post all the relevant HTML, starting from a <form or at least a <fieldset - Kiril S.
I have updated the HTML and start from <fieldset>. I hope this enough fo you. thanks - Asad
Please explain your problem explicitly. Are you see any result or your outputs are wrong? - hamed baziyad

1 Answers

-1
votes

Try smth like this:

Actions action = new Actions(driver);    
WebElement optionsList = driver.findElement(By.xpath("//span[contains(@class, 'k-dropdown-wrap')]"));
action.moveToElement(optionsList);

List<WebElement> options = driver.getElemets(By.xpath("//span[contains(@class, 'k-input')]"));
for(WebElement option : options) {
    if (option.getText().equals("Texas")) {
        option.click();
    }
}

Stop writing strange xpath to elements :)