arm
arm

Reputation: 451

Unable to locate a dropdown

I have following so far:

<div class="username-link-container" aria-expanded="true" aria-controls="userlink-dropdown_1" data-dropdown="userlink-dropdown_1">
<div class="inline-block vertical-align username-container">
<span class="username pointer-cursor" style="min-width:133px; text-align:right;">
SJZS KLFR
<img class="arrow vertical-align pointer-cursor" src="/img/header/red_arrow.png">
</span>
</div>
<div id="userlink-dropdown_1" class="f-dropdown dropdown-contents open f-open-dropdown" aria-autoclose="false" aria-hidden="false" tabindex="-1" data-dropdown-content="" style="position: absolute; left: -0.549927px; top: 55px;">
<a id="alinkManageAccount" href="/en/Home" target="_self">Manage Account</a>
<a id="alinkManageProxy" href="/en/abc/abc1/abc2">Manage Proxy</a>
<a id="alinkSignOut">Sign Out</a>
</div>

I need to get to Sign out option which is inside the dropdown.

I have this:

WebDriver driver=new FirefoxDriver();

driver.get("https://ab.com/");

System.out.println(driver.getTitle());
System.out.println(driver.getPageSource());

driver.findElement(By.id("txt-username")).sendKeys("Username");
driver.findElement(By.id("pwd-password")).sendKeys("Passw0rd");
driver.findElement(By.id("login-widget-submit")).click();

Select droplist = new Select(driver.findElement((By) (By.className("username-link-container")).findElement((SearchContext) By.id("userlink-dropdown_1"))));
droplist.selectByValue("Sign Out");

I am getting java.lang.ClassCastException error. Can you please help?

Upvotes: 1

Views: 80

Answers (1)

alecxe
alecxe

Reputation: 474191

Select class will only work for the select elements. In this case, you would need to select the item from the dropdown "manually":

// open up the dropdown
driver.findElement(By.cssSelector(".username-link-container")).click();

// select option
driver.findElement(By.linkText("Sign Out")).click();

You might also need to wait for the link to become clickable:

WebDriverWait wait = new WebDriverWait(driver, 15);  
WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("Sign Out")));
link.click();

Upvotes: 1

Related Questions