2017-07-21 16 views
2

マウスのホバー上にリストを表示するメニューがあります。ログアウトをクリックします。 私はいくつかのコードを書いたが、望ましい結果を得ることができませんでした。Seleniumでdiv-> ul-> liの要素を選択する方法

ここに私のJavaコードがあります:

public void Logout() throws Exception { 
     WebElement profileDropdown = driver.findElement(By.className("profile-dropdown")); 
     //profileDropdown.click(); 
     //profileDropdown.findElement(By.id("lnkLogout")).click(); 
     //Select oSelect = new Select(driver.findElement(By.className("profile-dropdown"))); 

     //oSelect.selectByVisibleText("Log Out"); 
     //List<WebElement> li = profileDropdown.findElements(By.id("lnkLogout")); 
     //li.get(0).click();//If there are only two such element, here 1 is index of 2nd element in list returned. 

     List<WebElement> elems = driver.findElements(By.cssSelector("ul>li>a")); 
     elems.get(5).click(); 
     //profileDropdown.findElement(By.xpath("(//a[contains(text(),'Log Out')])[2]")).click(); 

    } 

私は、あなたがコメントしているコードの行を見ることができ、多くのことを試してみました。何も私のために働く。

ここで私は

<div style="display: none;" class="profile-dropdown"> 
       <ul> 
        <li><a href="https://consumers.keenu.pk/index.php/profile/">My Profile <!--<label id="lblProfilePercentage">0</label>--></a></li> 
        <li><a href="https://consumers.keenu.pk/index.php/transactionhist/">Transaction History</a></li> 
        <li><a href="https://consumers.keenu.pk/index.php/customer-care/helpline">Helpline</a></li> 
        <li><a href="https://consumers.keenu.pk/index.php/pin-pass/">PIN &amp; Password</a></li> 
        <li><a href="https://consumers.keenu.pk/index.php/settings/">Favorites</a></li> 
        <li><a href="#" id="lnkLogout" style="cursor:pointer">Log Out</a></li> 
       </ul> 
      </div> 

のための自動化を行っていた私のHTMLコードは、「プロファイル - ドロップダウン」要素を見つけることができますが、例外とリスト要素を見つけることができませんがスローされます。

お願いします。

答えて

1

メニューでマウスを動かすとメニュー項目が表示され、それをクリックしても機能しないという場合は、以下を行う必要があります。

はログアウトメニュー項目(リンク)は、それに3.click

表示されるまで待つ2.needメニューにも、最初の

を合わせる1.mouse。

Actions action = new Actions(driver); 
action.moveToElement(profileDropdown).build().perform(); 
WebDriverWait wait = new WebDriverWait(driver, 30); 
WebElement logoutLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lnkLogout"))); 
logoutLink.click(); 
+0

予想条件が失敗した:By.xpathによって位置決め要素の可視性を待っている:(// [含まれている(テキスト()、 'ログアウト')])[2]()S(30秒間試み500ミリリットルの間隔で) – Asad

+0

あなたはIDで試してみることができます、私はまた、ブラウザを閉じて、動作を確認しないでください、あなたはメニュー項目を見ることができる答えを更新しましたか? –

+0

同じエラーとメニュー項目を表示することはできません。 – Asad

-1

jqueryを使用することをお勧めします。このコードは、jquery google cdnをヘッダーに含める場合にのみ機能します。 bodyタグの後にこのコードを入力してください。

<script> 
function whatever(){ 
$(".profile-dropdown > ul > li > #lnkLogout").click(); 
} 


</script> 
関連する問題