如何修复 Selenium 不清除输入上的默认文本

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


该字段的 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。


慕哥6287543
浏览 129回答 1
1回答

互换的青春

某些基于 JS 的 Web 应用程序可能无法正确清除输入字段。您可以尝试使用 Actions 类完全单击并清除字段。例如:elem&nbsp;=&nbsp;driver.findElement(By.id("amount"))actions.move_to_element(elem).click().build().perform()actions.send_keys(Keys.BACKSPACE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.send_keys(Keys.BACKSPACE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.send_keys(Keys.BACKSPACE) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.send_keys(Keys.BACKSPACE).build().perform()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python