Reputation: 71
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&url=/Sponsored">UAE</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=bh&url=/Sponsored">Bahrain</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=om&url=/Sponsored">Oman</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=qa&url=/Sponsored">Qatar</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=kw&url=/Sponsored">Kuwait</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=eg&url=/Sponsored">Egypt</a>
</li>
<li class="header__country-selector--desktop__country">
<a href="/change-country?to=jo&url=/Sponsored">Jordan</a>
I'am so new to Selenium and looking for a help
Upvotes: 0
Views: 104
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
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