Home > Blockchain >  How to "hit Enter" using driver.execute_script?
How to "hit Enter" using driver.execute_script?

Time:09-06

I'm trying to make an auto-login twitter bot. But when I try to send_keys to passwords field, I can't. (Only passwords field doesn't work, the similar code for send_keys to username and phone_number works).

Error Message: "selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable".

So I tried to use execute_script instead.

driver.execute_script("arguments[0].value=arguments[1];", password_input, TWITTER_PASSWORD)

The line above works too. But I don't know how to send Enter key to arguments[0]

driver.execute_script("arguments[0].submit();", password_input)

Tried this but doesn't work. (Forgive me if this line is completely wrong cause it looks like this method take JS code and I don't know JS)

Please help me with this. Any help for this problem or just my code in general would be appreciated.

""" If you want to test the code, remember to give values to TWITTER_EMAIL, TWITTER_PHONE_NUMBER and TWITTER_PASSWORD. """

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time

TWITTER_EMAIL = 
TWITTER_PHONE_NUMBER =
TWITTER_PASSWORD = 

URL = r"https://twitter.com/i/flow/login"

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)

driver.get(url=URL)


def twitter_auto_login():

    time.sleep(10)

    username_input = driver.find_element(By.CSS_SELECTOR, "input")
    username_input.send_keys(TWITTER_EMAIL)
    time.sleep(2)
    username_input.send_keys(Keys.ENTER)

    time.sleep(5)

    phone_num_input = driver.find_element(By.CSS_SELECTOR, "input")
    phone_num_input.send_keys(TWITTER_PHONE_NUMBER)
    time.sleep(2)
    phone_num_input.send_keys(Keys.ENTER)

    time.sleep(5)

    password_input = driver.find_element(By.CSS_SELECTOR, "input")
    # driver.execute_script("arguments[0].click();", password_input)
    driver.execute_script("arguments[0].value=arguments[1];", password_input, TWITTER_PASSWORD)
    # https://stackoverflow.com/questions/52273298/what-is-arguments0-while-invoking-execute-script-method-through-webdriver-in

    time.sleep(2)

    driver.execute_script("arguments[0].submit();", password_input)

UPDATE: So I found exactly what is my stupid mistake here, in the third time entering input (the password input time), username is also an input tag will appear again and will be an unreachable tag. just use find_elements and select the second element of that list will get us the right password input tag. But the solution from wado is better, just use it in case u face the same problem

CodePudding user response:

No need to use driver.execute_script. In your case you're just locating the elements in a wrong way. You should do something like this:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


TWITTER_EMAIL = "[email protected]"
TWITTER_PASSWORD = "lalala"

URL = r"https://twitter.com/i/flow/login"

s = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=s)

driver.get(url=URL)


def twitter_auto_login():
    username_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@autocomplete='username']")))
    username_input.send_keys(TWITTER_EMAIL)
    username_input.send_keys(Keys.ENTER)

    password_input = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[@autocomplete='current-password']")))
    password_input.send_keys(TWITTER_PASSWORD)
    password_input.send_keys(Keys.ENTER)


twitter_auto_login()

Note that I used explicit waits that are way better than those implicit waits (that makes you waste a lot of time senseless).

CodePudding user response:

Via JQuery, you can use the following Javascript to simulate the enter event:

driver.execute_script("var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
var e = $.Event( 'keypress', { which: 13 } );
arguments[0].trigger(e);", password_input)
  • Related