首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >无法对下拉菜单执行操作,因为通过sendKeys发送过期日期后会弹出日历

无法对下拉菜单执行操作,因为通过sendKeys发送过期日期后会弹出日历
EN

Stack Overflow用户
提问于 2018-06-01 00:20:32
回答 2查看 124关注 0票数 0

在我的代码中,我编写了这段代码,以便通过SendKeys输入到期日期

代码语言:javascript
复制
driver.findElement(By.cssSelector("#passport-exp-date-input-243202807"))
      .sendKeys("2021-05-13");

现在,在输入日期后,日历会弹出此日期,因此,我无法执行下一步操作,即选择下拉列表中的性别。

thar的代码如下:

代码语言:javascript
复制
String locator = "select[id='passport-gender-243202807']" + 
      "[data-sync-to-element='#citizenship-info-view-passport_gender-243202807']"
Select dropdownGender = new Select(driver.findElement(By.cssSelector()));

dropdownGender.selectByVisibleText("Female");

它给了我一个错误:“元素不可见”

到期日的格式:

代码语言:javascript
复制
 <input required="" type="text" id="passport-exp-date-input-243202807"
        name="documentExpirationDateInput" 
        class="form-control expirationDate input-sm orange-calendar 
               sync validDateFormat date-picker-selector hasDatepicker"
        data-language="en" data-date-format="yy-mm-dd" 
        data-end-date-of-trip="2018-07-07T00:00:00.000-04:00" 
        data-min-date="0" data-max-date="" 
        data-sync-to-element="#citizenship-info-view-document_expiration-243202807" 
        data-ocr="" value="">

性别的超文本标记:

代码语言:javascript
复制
<select required="" type="text" name="gender" id="passport-gender-243202807"
        class="form-control input-sm sync" data-ocr=""
        data-sync-to-element="#citizenship-info-view-passport_gender-243202807">
    <option value="">Gender</option>
    <option value="M">Male</option>
    <option value="F">Female</option>
</select>

性别的屏幕截图

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-01 11:11:04

代码语言:javascript
复制
// input expiration date
driver.findElement(By.cssSelector("#passport-exp-date-input-243202807"))
      .sendKeys("2021-05-13");

// click any element on page which is not covered by the calendar pop-up
// to make the calendar close, so that it won't cover the Gender element
driver.findElement(<any element no covered by calendar>).click()

// if it's native dropdown,
// click on the dropdown to expand all options
// Generally, for native dropdown, no need to expand options
// we can directly click the option without expand all options.
driver.findElement(By.id("passport-gender-243202807")).click()

// click the wanted option
driver.findElement(By.id("passport-gender-243202807"))
      .findElement(By.xpath("./option[text()='Female']")).click()
票数 0
EN

Stack Overflow用户

发布于 2018-06-01 05:33:18

我还没有测试过,但我非常确定它应该可以工作:

代码语言:javascript
复制
Select dropdown = new Select(driver.findElement(By.id("passport-gender-243202807")));
dropdown.selectByVisibleText("Female");

或者,如果按文本选择不起作用,请尝试按索引选择:

代码语言:javascript
复制
dropdown.selectByIndex(2);

此外,要在其他一些可见元素上自动触发单击事件以使弹出日历消失,请尝试执行以下操作:https://stackoverflow.com/a/11956130/8065825

更新:

我刚刚注意到你的dropdown HTML中有一个错误:你把</select>结束标签放在下拉选项之前,这太明显了,这是行不通的。试着这样做:

代码语言:javascript
复制
<select required="" type="text" name="gender" id="passport-gender-243202807" class="form-control input-sm sync" data-sync-to-element="#citizenship-info-view-passport_gender-243202807" data-ocr="">
    <option value="">Gender</option>
    <option value="M">Male</option>
    <option value="F">Female</option>
</select>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50628730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档