Home > Mobile >  I'm trying to write a simple whatsapp bot with selenium but the messaging loop won't work
I'm trying to write a simple whatsapp bot with selenium but the messaging loop won't work

Time:09-16

    textbar = driver.find_element("xpath",'//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p')
    while(True):
        message = input("Please enter the text you want to  send to the selected person to stop the program type -exit- : ")    
        textbar.send_keys(message)
        if(message == "exit"):
            break
        textbar.send_keys(Keys.RETURN)

CodePudding user response:

In your code, you're calling textbar.send_keys(message) before checking if the message is exit. You should move up your if statement so that you exit the program sooner. And put the find_element inside the loop to avoid StaleElementReferenceException. Eg:

while(True):
    message = input("Please enter the text you want to send to the selected person. To stop the program, type -exit- : ")
    if(message == "exit"):
        break
    textbar = driver.find_element("xpath",'//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p')
    textbar.send_keys(message)
    textbar.send_keys(Keys.RETURN)
  • Related