-1

Code trials:

def send_unsaved_contact_message():
    global message
    try:
        time.sleep(10)
        browser.implicitly_wait(10)
        #input_box = (By.XPATH,"//div[text()='Type a message']")
        #input_box = (By.XPATH,"//div[@class='p3_M1']")
        pyperclip.copy(message)
        input_box = wait.until(EC.presence_of_element_located((By.XPATH,
        "(//div[@title='Type a message'])[1]"))) #//div[@class='fd365im1 to2l77zo bbv8nyr4 mwp4sxku gfz4du6o ag5g9lrv']
        #pyperclip.copy(message) # get text in clipboard 
        #for ch in message:
            #if ch == "\n":
                #action.key_down(Keys.SHIFT).key_down(Keys.ENTER).key_up(Keys.ENTER).key_up(
                    #Keys.SHIFT).key_up(Keys.BACKSPACE).perform()
            #else:
        #input_box.send_keys(ch)
        #browser.execute_script(JS_ADD_TEXT_TO_INPUT, input_box, message)  
        action.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()           
        #action.send_keys(Keys.CONTROL,"v").perform()
        whatsapp_send_button = browser.find_element(By.XPATH,
        "//span[contains(@data-icon,'send')]")
        browser.execute_script("arguments[0].click();", whatsapp_send_button)
    except Exception as e:
        print("Failed to send message exception: ", e)
        return

The above code sends half of my message. As an example when my message is:

hi
my name is setareh
this is my group link

Selenium sends my message like as follows:

hi
my name is setareh

What is the problem?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

To send the desired text to the <input> box instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

input_box = wait.until(EC.element_to_be_clickable((By.XPATH, "(//div[@title='Type a message'])")))

or

input_box = wait.until(EC.element_to_be_clickable((By.XPATH, "(//div[@title='Type a message'])[1]")))
  • 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