CookieMan
CookieMan

Reputation: 71

Method to select drop down values in selenium

I need a simple method to send values to "dropID"

values are

UAE, Bahrain,Oman... etc

drop down UI

enter image description here

my xpath(global)

//*[@id="wrapper"]/div[1]/header/div[2]/div[2]/div[1]/div[1]/div[2]/nav/ul/li[' + **dropID** + ']/a

HTML

<nav class="header__countries-menu--desktop mi-js-countries-menu-desktop" style="display: block;">
                  <ul>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=ae&amp;url=/Sponsored">UAE</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=bh&amp;url=/Sponsored">Bahrain</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=om&amp;url=/Sponsored">Oman</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=qa&amp;url=/Sponsored">Qatar</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=kw&amp;url=/Sponsored">Kuwait</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=eg&amp;url=/Sponsored">Egypt</a>
                    </li>
                    <li class="header__country-selector--desktop__country">
                      <a href="/change-country?to=jo&amp;url=/Sponsored">Jordan</a>

I'am so new to Selenium and looking for a help

Upvotes: 0

Views: 104

Answers (2)

CookieMan
CookieMan

Reputation: 71

Below is the code that i have tried

    public void portfolioRenewalSearch(String portfolioId) throws Exception {

             By xpath2 = By.xpath("//*[@id='wrapper']/div[1]/header/div[2]/div[2]/div[1]/div[1]/div[2]/nav/ul/li[" + portfolioId + "]/a");

           //MY ACTION IS HERE

            return;
}

then i called from my test cases

Upvotes: 0

Mike ASP
Mike ASP

Reputation: 2333

(1) Do not use absolute paths , it's bad practice. Please see below some of dynamic xpath examples:

Lets say you have this under your CountryPage class

WebElement countryEle = driver.findElement(By.xpath("//[@class='header__countries-menu--desktop mi-js-countries-menu-desktop']//[contains(text(),'"+dynamicText+"')]") ;

or

WebElement countryEle = driver.findElement(By.xpath("//[@class='header__country-selector--desktop__country']//[contains(text(),'"+dynamicText+"')]") ;

(2) You need to click on drop down in order to click any country:

driver.findElement(By.xpath("dropDown of your country ")).click();

(3) send your dynamic DropID to select country:

public static void selectFromDropdown(WebDriver driver, WebElement element)
{
    driver.findElement(element).click();
}

(4) call your method:

selectFromDropdown(driver,CountryPage(driver,"UAE"))

Upvotes: 1

Related Questions