2017-03-16 65 views
0

我試圖使用硒的webdriver在Gmail註冊的Web應用程序中提取文本從出生月份字段(一個月的名稱,如一月,二月...)文本。代碼如下所示:獲得使用硒嵌套div標籤的webdriver

<label id="month-label" class="month"> 
    <span id="BirthMonth" class=" form-error" aria-invalid="true"> 
    <div class="goog-inline-block goog-flat-menu-button jfk-select" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" aria-activedescendant=":0" title="Birthday"> 
     <div class="goog-menu goog-menu-vertical" style="-moz-user-select: none; visibility: visible; left: 0px; top: -299.133px; display: none;" role="listbox" aria-haspopup="true"> 
     <div id=":1" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div class="goog-menuitem-content">January</div> 
     </div> 
     <div id=":2" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div class="goog-menuitem-content">February</div> 
     </div> 
     <div id=":3" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div class="goog-menuitem-content">March</div> 
      </div> 
     <div id=":4" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
      <div class="goog-menuitem-content">April</div> 
     </div> 
     <div id=":5" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":6" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":7" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":8" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":9" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":a" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":b" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     <div id=":c" class="goog-menuitem" role="option" style="-moz-user-select: none;"> 
     </div> 
    <input id="HiddenBirthMonth" name="BirthMonth" type="hidden"> 
    </span> 
</label> 

我嘗試起訴xpath和其他元素定位器,但無法提取文本。當我使用Select類時,出現錯誤cannot apply select with div。請提出一些解決方案。

+0

分享你的代碼你試過的東西 – thebadguy

+0

嘗試了以下代碼:Select months = new Select(driver.findbyelement(By.xpath(「// div [@class =''goog-menu goog-menu-vertical] /格「))); 列表 months_options = months.getOptions(); (i)0; i

+1

不能使用'選擇()'和'div'元素,用簡單的'點擊()',而不是 – Andersson

回答

0

這裏是你必須做的: 月份產品自舉下拉。所以你必須遍歷它才能得到清單。這裏是工作代碼:

package Gmail; 
import java.util.List; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 


public class gmailLogin { 

    public static void main(String[] args) throws InterruptedException { 


     System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.manage().window().maximize(); 
     driver.get("http://gmail.com"); 
     driver.findElement(By.xpath(".//*[@id='link-signup']/a")).click(); 
     Thread.sleep(2000); 
     driver.findElement(By.id("BirthMonth")).click(); 
     Thread.sleep(2000); 
     List<WebElement> month_menu = driver.findElements(By.xpath(".//div[@class='goog-menuitem-content']")); 
     for (int i=0;i<month_menu.size();i++) 
     { 
      WebElement element = month_menu.get(i); 
      String contents = element.getAttribute("innerHTML"); 
      System.out.println("Values from dropdown is : "+contents); 
     } 
     driver.quit(); 

    } 

} 

讓我知道這是否對你有幫助。

+0

感謝您的答覆...肯定會嘗試,讓你知道的狀態.. –

+0

@AfsarMohammed如果這個答案已經解決了您的查詢請關閉話題接受答案。謝謝。 – DebanjanB

+0

謝謝Dev ..你的邏輯很簡單,工作很順利......你做了我的一天。 –