硒:无法选择下拉 [英] Selenium: Unable to Select DropDown

查看:23
本文介绍了硒:无法选择下拉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

HTML 代码如下所示:

<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 term-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"></表单>

<p class="为什么-信息">

<div class="side-content">

<div class="google-footer-bar">

我尝试使用以下所有方法,但 Selenium 每次都会抛出错误:

代码 1:

 driver.get("https://accounts.google.com/signup");Select select = new Select(driver.findElement(By.xpath(".//[@id='BirthMonth']/div")));select.deselectAll();select.selectByVisibleText("April");

代码 2:

driver.get("https://accounts.google.com/signup");Select select = new Select(driver.findElement(By.xpath(".//[@id='BirthMonth']")));select.deselectAll();select.selectByVisibleText("April");

我尝试了一些其他变体,但都没有奏效.我知道我需要获取选择"值,但是当我尝试使用 Firebug 获取此值时,我在 HTML 中找不到任何此类值.

我正在使用 Java.

什么是正确的 XPath 使用?

谢谢

解决方案

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

更新:

尝试以下操作:

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

I am trying to select the month on Gmail signup page using Selenium.

The HTML code looks like this:

<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>

I tried using all of the following but Selenium throws error each time:

Code 1:

    driver.get("https://accounts.google.com/signup");
    Select select = new Select(driver.findElement(By.xpath(".//[@id='BirthMonth']/div")));
    select.deselectAll();
    select.selectByVisibleText("April");

Code 2:

driver.get("https://accounts.google.com/signup");
    Select select = new     Select(driver.findElement(By.xpath(".//[@id='BirthMonth']")));     
    select.deselectAll();
    select.selectByVisibleText("April");

I tried some other variations but none worked. I understand I need to get the "Select" value however when I try obtaining this value using Firebug I don't find any such value in HTML.

I am using Java.

What is correct XPath to use?

Thanks

解决方案

Apparently, you are trying to use the Select class to manipulate a standard UL element. The Select class will work only for dropdown elements. It seems Google is making the list look like a dropdown while it is a regular element. The HTML code certainly does not contain any Select items.

Update:

Try the following:

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

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

这篇关于硒:无法选择下拉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Java开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆