问题:用硒清除textarea中的文本

我进行了一些测试,这些测试用于检查某些字段中的文本无效时是否出现了正确的错误消息。有效性检查之一是某个textarea元素不为空。

如果此文本区域中已经有文本,我如何告诉硒清除该字段?

就像是:

driver.get_element_by_id('foo').clear_field()

I’ve got some tests where I’m checking that the proper error message appears when text in certain fields are invalid. One check for validity is that a certain textarea element is not empty.

If this textarea already has text in it, how can I tell selenium to clear the field?

something like:

driver.get_element_by_id('foo').clear_field()

回答 0

driver.find_element_by_id('foo').clear()
driver.find_element_by_id('foo').clear()

回答 1

您可以使用

 webElement.clear();

如果此元素是文本输入元素,则将清除该值。

请注意,此事件引发的事件可能与您预期的不同。特别是,我们不会触发任何键盘或鼠标事件。如果您想确保触发了键盘事件,请考虑使用sendKeys(CharSequence)。例如:

 webElement.sendKeys(Keys.BACK_SPACE); //do repeatedly, e.g. in while loop

要么:

 webElement.sendKeys(Keys.CONTROL + "a");
 webElement.sendKeys(Keys.DELETE);

You can use

 webElement.clear();

If this element is a text entry element, this will clear the value.

Note that the events fired by this event may not be as you’d expect. In particular, we don’t fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider using something like sendKeys(CharSequence). E.g.:

 webElement.sendKeys(Keys.BACK_SPACE); //do repeatedly, e.g. in while loop

or:

 webElement.sendKeys(Keys.CONTROL + "a");
 webElement.sendKeys(Keys.DELETE);

回答 2

我遇到了.clear()无法正常工作的领域。结合使用前两个答案可解决此问题。

from selenium.webdriver.common.keys import Keys

#...your code (I was using python 3)

driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a");
driver.find_element_by_id('foo').send_keys(Keys.DELETE);

I ran into a field where .clear() did not work. Using a combination of the first two answers worked for this field.

from selenium.webdriver.common.keys import Keys

#...your code (I was using python 3)

driver.find_element_by_id('foo').send_keys(Keys.CONTROL + "a");
driver.find_element_by_id('foo').send_keys(Keys.DELETE);

回答 3

在最新的Selenium版本中,使用:

driver.find_element_by_id('foo').clear()

In the most recent Selenium version, use:

driver.find_element_by_id('foo').clear()

回答 4

对于java

driver.findelement(By.id('foo').clear();

要么

webElement.clear();

如果此元素是文本输入元素,则将清除该值。

for java

driver.findelement(By.id('foo').clear();

or

webElement.clear();

If this element is a text entry element, this will clear the value.


回答 5

这是一般语法

driver.find_element_by_id('Locator value').clear();
driver.find_element_by_name('Locator value').clear();

It is general syntax

driver.find_element_by_id('Locator value').clear();
driver.find_element_by_name('Locator value').clear();

回答 6

通过对clear()的简单调用,在DOM中就会出现相应的输入/文本区域组件仍具有其旧值的情况,因此对该组件进行的任何后续更改(例如,用新值填充该组件)都不会及时得到处理。

如果看一下硒源代码,您会发现clear()方法记录在案并带有以下注释:

/ **如果此元素是文本输入元素,则将清除该值。对其他元素没有影响。文本输入元素是INPUT和TEXTAREA元素。请注意,此事件引发的事件可能与您预期的不同。特别是,我们不会触发任何键盘或鼠标事件。如果要确保触发键盘事件,请考虑将{@link #sendKeys(CharSequence …)}之类的键与退格键一起使用。为确保您收到更改事件,请考虑使用Tab键跟随对{@link #sendKeys(CharSequence …)}的调用。* /

因此,使用此有用的提示来清除输入/文本区域(已经具有值的组件)并为其分配新的值,您将获得如下代码:

public void waitAndClearFollowedByKeys(By by, CharSequence keys) {
    LOG.debug("clearing element");
    wait(by, true).clear();
    sendKeys(by, Keys.BACK_SPACE.toString() + keys);
}

public void sendKeys(By by, CharSequence keysToSend) {
    WebElement webElement = wait(by, true);
    LOG.info("sending keys '{}' to {}", escapeProperly(keysToSend), by);
    webElement.sendKeys(keysToSend);
    LOG.info("keys sent");
}

private String escapeProperly(CharSequence keysToSend) {
    String result = "" + keysToSend;
    result = result.replace(Keys.TAB, "\\t");
    result = result.replace(Keys.ENTER, "\\n");
    result = result.replace(Keys.RETURN, "\\r");

    return result;
}

抱歉,此代码是Java而不是Python。另外,我还不得不跳过其他的“ waitUntilPageIsReady()-方法,这会使这篇文章过长。

希望这对您在硒的旅途中有所帮助!

With a simple call of clear() it appears in the DOM that the corresponding input/textarea component still has its old value, so any following changes on that component (e.g. filling the component with a new value) will not be processed in time.

If you take a look in the selenium source code you’ll find that the clear()-method is documented with the following comment:

/** If this element is a text entry element, this will clear the value. Has no effect on other elements. Text entry elements are INPUT and TEXTAREA elements. Note that the events fired by this event may not be as you’d expect. In particular, we don’t fire any keyboard or mouse events. If you want to ensure keyboard events are fired, consider using something like {@link #sendKeys(CharSequence…)} with the backspace key. To ensure you get a change event, consider following with a call to {@link #sendKeys(CharSequence…)} with the tab key. */

So using this helpful hint to clear an input/textarea (component that already has a value) AND assign a new value to it, you’ll get some code like the following:

public void waitAndClearFollowedByKeys(By by, CharSequence keys) {
    LOG.debug("clearing element");
    wait(by, true).clear();
    sendKeys(by, Keys.BACK_SPACE.toString() + keys);
}

public void sendKeys(By by, CharSequence keysToSend) {
    WebElement webElement = wait(by, true);
    LOG.info("sending keys '{}' to {}", escapeProperly(keysToSend), by);
    webElement.sendKeys(keysToSend);
    LOG.info("keys sent");
}

private String escapeProperly(CharSequence keysToSend) {
    String result = "" + keysToSend;
    result = result.replace(Keys.TAB, "\\t");
    result = result.replace(Keys.ENTER, "\\n");
    result = result.replace(Keys.RETURN, "\\r");

    return result;
}

Sorry for this code being Java and not Python. Also, I had to skip out an additional “waitUntilPageIsReady()-method that would make this post way too long.

Hope this helps you on your journey with Selenium!


回答 7

以我的经验,这是最有效的

driver.find_element_by_css_selector('foo').send_keys(u'\ue009' + u'\ue003')

我们正在发送Ctrl +退格键以删除输入中的所有字符,您也可以使用delete替换退格键。

编辑:删除键依赖

In my experience, this turned out to be the most efficient

driver.find_element_by_css_selector('foo').send_keys(u'\ue009' + u'\ue003')

We are sending Ctrl + Backspace to delete all characters from the input, you can also replace backspace with delete.

EDIT: removed Keys dependency


回答 8

driver.find_element_by_xpath("path").send_keys(Keys.CONTROL + u'\ue003') 与FireFox一起工作得很好

  • u’\ ue003’对像我这样的人来说是BACK_SPACE-永远不会记住它)

driver.find_element_by_xpath("path").send_keys(Keys.CONTROL + u'\ue003') worked great with FireFox

  • u’\ue003′ is a BACK_SPACE for those like me – never remembering it)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。