到目前为止我已经关注:
<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>
我需要登录下拉列表中的退出选项。
我有这个:
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");
我收到java.lang.ClassCastException
错误。
你能帮忙吗?
答案 0 :(得分:1)
Select
课程仅适用于select
元素。在这种情况下,您需要从下拉列表中选择项目&#34;手动&#34;:
// open up the dropdown
driver.findElement(By.cssSelector(".username-link-container")).click();
// select option
driver.findElement(By.linkText("Sign Out")).click();
您可能还需要等待链接变为可点击:
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("Sign Out")));
link.click();