Clear text from textarea with selenium

后端 未结 9 1287
时光说笑
时光说笑 2020-12-02 09:13

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 el

相关标签:
9条回答
  • 2020-12-02 09:19

    for java

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

    or

    webElement.clear();
    

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

    0 讨论(0)
  • 2020-12-02 09:20

    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!

    0 讨论(0)
  • 2020-12-02 09:31

    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)
    0 讨论(0)
  • 2020-12-02 09:34

    In the most recent Selenium version, use:

    driver.find_element_by_id('foo').clear()
    
    0 讨论(0)
  • 2020-12-02 09:38

    It is general syntax

    driver.find_element_by_id('Locator value').clear();
    driver.find_element_by_name('Locator value').clear();
    
    0 讨论(0)
  • 2020-12-02 09:38

    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

    0 讨论(0)
提交回复
热议问题