如何修复硒未清除输入上的默认文本



我正在尝试在包含默认字符串的文本字段中输入金额。

该字段的

html:

<div class="InputGroup">
    <span class="InputGroup-context">$</span>
    <input autocomplete="off" class="Input InputGroup-input" id="amount" name="amount" type="text" maxlength="12" value="0.00">
</div>

尝试将文本输入字段时,而不是替换默认文本,它将其附加到它。

我尝试使用 amount.clear()(金额是我所说的元素),但是在运行并发送密钥后,它会引发以下例外:

selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document

您将从我当前的代码中看到,我也试图双击默认文本,但这无济于事。

这是我目前的代码:

ActionChains(driver).move_to_element(amount).click(amount).click(amount).perform()
amount.send_keys(Keys.BACKSPACE)
amount.send_keys('100')

当我期望100时,它导致输入字段为0.00100。

某些基于JS的网络应用程序可能无法正确清除输入字段。

您可以尝试使用动作类单击并完全清除字段。例如:

elem = driver.findElement(By.id("amount"))
actions.move_to_element(elem).click().build().perform()
actions.send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE)
       .send_keys(Keys.BACKSPACE).build().perform()

最新更新