1

I need to send a comment in the field. It works with "input", but not with "textarea". Someone had to deal with this?

Before clicking on the Html element it looks like:

<textarea class = '1' aria-label="Add a comment..." placeholder="Add a comment..."autocomplete="off"  autocorrect="off"> </textarea>

Then:

<textarea class = '1 focus-visible' aria-label="Add a comment..." placeholder="Add a comment..."autocomplete="off"  autocorrect="off" style="height: 18px;"  data-focus-visible-added = ""> </textarea>

The textarea field is activated, but no comment is added.

browser.find_element_by_css_selector('textarea[placeholder="Add a comment..."]').send_keys('comment')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

I'm not sure to understand what you mean but for me this code works perfectly fine

class Bot():
    def __init__(self):
       self.driver = webdriver.Chrome()
    def comment(self):
       self.driver.get(url)
       self.driver.find_element_by_css_selector('textarea[placeholder="Add a comment..."]').send_keys('comment')

bot = Bot()
bot.comment()

It write in this element on my web page:

<textarea class = '1' aria-label="Add a comment..." placeholder="Add a comment..."autocomplete="off"  autocorrect="off"> </textarea>
Dusart Victor
  • 76
  • 3
  • 11
0

To send a character sequence to the text area you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.1[aria-label^='Add a comment'][placeholder^='Add a comment']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "textarea.1.focus-visible[aria-label^='Add a comment'][placeholder^='Add a comment'][data-focus-visible-added]"))).send_keys("Алекс")
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[starts-with(@aria-label, 'Add a comment') and starts-with(@placeholder, 'Add a comment')][@class='1']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//textarea[starts-with(@aria-label, 'Add a comment') and starts-with(@placeholder, 'Add a comment')][@class='1 focus-visible' and @data-focus-visible-added]"))).send_keys("Алекс")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352