Java Selenium:无法选择下拉列表

Java Selenium:无法选择下拉列表,java,selenium,xpath,Java,Selenium,Xpath,我正在尝试使用Selenium在Gmail注册页面上选择月份 HTML代码如下所示: <div class="signup-box"> <form id="createaccount" class="createaccount-form" method="post" action="SignUp?dsh=8533946547080886952&service" name="createaccount"> <input id="timeStmp" type="

我正在尝试使用Selenium在Gmail注册页面上选择月份

HTML代码如下所示:

<div class="signup-box">
<form id="createaccount" class="createaccount-form" method="post" action="SignUp?dsh=8533946547080886952&service" name="createaccount">
<input id="timeStmp" type="hidden" value="1437641960669" name="timeStmp"/>
<input id="secTok" type="hidden" value=".AG5fkS9XSKPG0WTmVtKSa4sso_d_jcxGzQ==" name="secTok"/>
<input id="dsh" type="hidden" value="8533946547080886952" name="dsh"/>
<input id="ktl" type="hidden" value="A" name="ktl"/>
<input id="ktf" type="hidden" name="ktf" value="FirstName LastName GmailAddress Passwd PasswdAgain RecoveryEmailAddress "/>
<input id="_utf8" type="hidden" value="☃" name="_utf8"/>
<input id="bgresponse" type="hidden" value="js_disabled" name="bgresponse"/>
<div id="name-form-element" class="form-element multi-field name">
<div id="gmail-address-form-element" class="form-element email-address">
<div id="password-form-element" class="form-element">
<div id="confirm-password-form-element" class="form-element">
<div id="birthday-form-element" class="form-element multi-field birthday">
<fieldset>
<legend>
<label id="month-label" class="month">
<span id="BirthMonth">
<div class="goog-inline-block goog-flat-menu-button jfk-select goog-flat-menu-button-hover" role="listbox" style="-moz-user-select: none;" aria-expanded="false" tabindex="0" aria-haspopup="true" aria-activedescendant=":0" title="">
<input id="HiddenBirthMonth" type="hidden" name="BirthMonth"/>
</span>
</label>
<label id="day-label" class="day">
<label id="year-label" class="year">
</fieldset>
<span id="errormsg_0_BirthMonth" class="errormsg" role="alert"/>
<span id="errormsg_0_BirthDay" class="errormsg" role="alert"/>
<span id="errormsg_0_BirthYear" class="errormsg" role="alert"/>
</div>
<div id="gender-form-element" class="form-element">
<div id="phone-form-element" class="form-element">
<div id="recovery-email-form-element" class="form-element recovery-email">
<div class="form-element">
<div id="country-code-form-element" class="form-element">
<div id="termsofservice-form-element" class="form-element terms-of-service">
<div id="extra-tos" class="form-element"/>
<input id="timeStmp2" type="hidden" value="1437641960669" name="timeStmp2"/>
<input id="secTok2" type="hidden" value=".AG5fkS-iqLwOSgeM3Qqu9w7rAxVOlzyJGw==" name="secTok2"/>
<div class="form-element nextstep-button">
</form>
</div>
<p class="why-information">
</div>
<div class="side-content">
</div>
</div>
<div class="google-footer-bar">
</div>
代码2:

driver.get("https://accounts.google.com/signup");
    Select select = new     Select(driver.findElement(By.xpath(".//[@id='BirthMonth']")));     
    select.deselectAll();
    select.selectByVisibleText("April");
我尝试了一些其他的变化,但都不起作用。我知道我需要获取“Select”值,但是当我尝试使用Firebug获取该值时,我在HTML中找不到任何这样的值

我正在使用Java

什么是正确的XPath


很显然,您正在尝试使用Select类来操作标准UL元素。Select类仅适用于下拉元素。 谷歌似乎正在让这个列表看起来像一个下拉列表,而它是一个常规元素。HTML代码当然不包含任何选择项

更新:

请尝试以下操作:

driver.findElement(By.xpath(".//*[@id='BirthMonth']/div")).click();

//Select April
driver.findElement(By.xpath(".//*[@id=':4']/div")).click();

只需按如下方式单击它,将第二个报表中的数字更改为您要查找的月份

driver.findElement(By.id("BirthMonth")).click();
driver.findElement(By.id(":1")).click();
如果跨度不可单击,您可以通过id获取元素并通过onclick事件获取div:

WebElement monthSelect = driver.findElement(By.id("BirthMonth"));
monthSelect.findElement(By.xpath("div[1]")).click();
driver.findElement(By.id(":1")).click();

据我所知,
Select
仅适用于带有
标记的html元素,但您正在尝试查找
元素

此外,您的xpath语句似乎不正确。您可以通过以下方式找到元素:

WebElement divElement = find(By.xpath("//span [@id='BirthMonth']/div"))

首先单击下拉列表:

  driver.findElement(By.xpath(".//div[@id='BirthMonth']/div[1]")).click();
然后从下拉列表中选择特定月份

例如,我想选择四月:

driver.findElement(By.xpath(".//*[@id='BirthMonth']/div[2]/div[@id=':4']/div").click();

要选择四月月份,还可以使用以下选项:

   driver.findElement(By.xpath("//div[@id=':1']"));
   driver.findElement(By.xpath("//div[@id=':1']"));