Clear text from textarea with selenium

后端 未结 9 1288
时光说笑
时光说笑 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:39

    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);
    
    0 讨论(0)
  • 2020-12-02 09:42
    driver.find_element_by_id('foo').clear()
    
    0 讨论(0)
  • 2020-12-02 09:45

    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);
    
    0 讨论(0)
提交回复
热议问题