在我的代码中,我编写了这段代码,以便通过SendKeys输入到期日期
driver.findElement(By.cssSelector("#passport-exp-date-input-243202807"))
.sendKeys("2021-05-13");
现在,在输入日期后,日历会弹出此日期,因此,我无法执行下一步操作,即选择下拉列表中的性别。
thar的代码如下:
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");
它给了我一个错误:“元素不可见”
到期日的格式:
<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="">
性别的超文本标记:
<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>
性别的屏幕截图
发布于 2018-06-01 11:11:04
// 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()
发布于 2018-06-01 05:33:18
我还没有测试过,但我非常确定它应该可以工作:
Select dropdown = new Select(driver.findElement(By.id("passport-gender-243202807")));
dropdown.selectByVisibleText("Female");
或者,如果按文本选择不起作用,请尝试按索引选择:
dropdown.selectByIndex(2);
此外,要在其他一些可见元素上自动触发单击事件以使弹出日历消失,请尝试执行以下操作:https://stackoverflow.com/a/11956130/8065825
更新:
我刚刚注意到你的dropdown HTML中有一个错误:你把</select>
结束标签放在下拉选项之前,这太明显了,这是行不通的。试着这样做:
<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>
https://stackoverflow.com/questions/50628730
复制相似问题