[英]Selenium - Sending text to a formatted input
在Java中使用Selenium WebDriver,我試圖將文本發送到如下所示的格式化輸入
___-__-____
您可能已經猜到了,這是“社會保障”字段。 無論如何,當我嘗試使用Selenium將密鑰發送到該字段時,它在查找元素時不會遇到麻煩,但是它不會發送任何密鑰,而是嘗試進行自動化的下一步。
我正在查找元素by.ID
,它的元素id
為ssn
(此元素唯一),因此我知道這里的問題不在於我的選擇方法。
這是元素的HTML和絕對XPath:
Absolute Xpath: html/body/div[2]/p[3]/input
<body>
<div id="logo">
<img src="/assets/images/logo.png">
</div>
<br>
<p id="errorsField" class="hidden">
<strong></strong>
</p>
<input class="hidden" type="text" id="clientUrlPart" value="bigco" />
<input class="hidden" type="text" id="is-self-registration" value="0" />
<div id="login">
<p>
<label for="firstName" class="required">First Name</label>
<input autocomplete="off" type="text" id="firstName" maxlength="25" value="">
</p>
<p>
<label for="lastName" class="required">Last Name</label>
<input autocomplete="off" type="text" id="lastName" maxlength="25" value="">
</p>
<p>
<label for="ssn" class="required">Social Security Number</label>
<input autocomplete="off" type="text" id="ssn" value="" filledSSN="">
</p>
<div id="newAccountInfo" class="hidden">
<div id="self-registration" class="hidden">
...
</body>
有什么想法嗎?
相關代碼:
public static void registerEmployee(String firstName, String lastName, String ssn, String dateOfBirth, String address,
String city, String state, String zip, String phone, String username, String password) {
SeleniumCommands.waitVisibleIdAndSendKeys(FIRSTNAME, firstName);
SeleniumCommands.waitVisibleIdAndSendKeys(LASTNAME, lastName);
SeleniumCommands.waitVisibleIdAndSendKeys(SSN, ssn);
...
}
public static final String SSN = "ssn";
public static void waitVisibleIdAndSendKeys(String id, String keys) {
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id))).sendKeys(keys);
}
有時,我經歷過這樣的預格式化文本框,需要一次輸入一個單獨的鍵,而不是Selenium / webdriver通常是如何輸入的。 嘗試創建三個語句以分別輸入一個數字以查看是否喜歡。 如果是這樣,您可以對ssn中的x進行一個for循環,以遍歷字符而無需先拆分它們,或者類似的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.