2

I'm trying to select an option from the DropDown using selenium java. I have also tried many solutions. The country element is found when executing XPath in FirePath but not finding while running script.

Selenium Code:

    driver.findElement(By.xpath("//*[@class='selectize-input items not-full has-options']")).click();
    Thread.sleep(500);

    String countryXpath = "//*[@class='select-item']/following::span[text()='India']";
    System.out.println(countryXpath);
    WebElement countryName = driver.findElement(By.xpath(countryXpath));

    ScrollHelper.ScrollHorizontalUpToVisibilityOfElement(countryName);
    countryName.click();

HTML Code:

<select class="selectized isfocused parsley-error" name="Country" data-input-parsley="" data-parsley-trigger="change input" data-parsley-required-message="This field should not empty" data-parsley-required="true" data-parsley-group="country-select" tabindex="-1" style="display: none;" data-parsley-id="17">
    <option value="" selected="selected"/>
</select>
<div class="selectize-control single">
    <div class="selectize-input items has-options not-full focus input-active dropdown-active">
        <input autocomplete="off" tabindex="" style="width: 100%; opacity: 1; position: relative; left: 0px;" placeholder="" type="text"/>
    </div>
    <div class="selectize-dropdown single" style="display: block; visibility: visible; width: 413px; top: 32px; left: 0px;">
    <div class="selectize-dropdown-content">
    <div class="select-item" data-selectable="" data-value="6">
        <div class="select-option">
            <span class="text">   Australia </span>
        </div>
    </div>
    <div class="select-item" data-selectable="" data-value="2">
        <div class="select-option">
            <span class="text">   Canada </span>
        </div>
    </div>
    <div class="select-item" data-selectable="" data-value="21">
        <div class="select-option">
            <span class="text">   China </span>
        </div>
    </div>
    <div class="select-item" data-selectable="" data-value="22">
        <div class="select-option">
            <span class="text">   India </span>
        </div>
    </div>
    <div class="select-item" data-selectable="" data-value="25">
        <div class="select-option">
            <span class="text">   USA </span>
        </div>
    </div>
</div>

enter image description here

3
  • which country you wanna select ? Commented May 3, 2018 at 10:40
  • 1
    Can you update the question with a bit more of the outerHTML to include the element with text as Country of residence? Commented May 3, 2018 at 10:46
  • actually, I want to select a country name is 'Singapore' which is at end of the list. Commented May 3, 2018 at 10:51

3 Answers 3

3

If you are able to click on Country of residence , then you have to scroll down little bit and then this code might work for you :

List<WebElement> countries = driver.findElements(By.cssSelector("div[class='select-option']>span"));
for(WebElement country : countries){
if(country.getText().trim().equals("Singapore"))
country.click();
}  

For scroll down if you are facing any issue, then please let me know.

Sign up to request clarification or add additional context in comments.

Comments

2

In Html, span text has spaces before country name

  <span class="text">   India </span>

Update the xpath as

String countryXpath = "//*[@class='select-item']/following::span[contains(text(), 'India')]";

Above mentioned xpath will check if the text of the span contains India

Comments

0

Try using this xpath to click on element:

"//span[contains(text(), 'India')]"

Let me know if it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.