简体   繁体   中英

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

在此处输入图片说明

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

(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"))

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM