Forums

(selenium) send_keys doesn't work in pythonanywhere

hallo guys i am trying to fill some google form with selenium, i tested the code below on pycharm and it works fine

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)

browser.get("https://docs.google.com/forms/d/e/1FAIpQLSfKIJCTUbSzYZmvFqY6jRS0RVvRQZZ_nkL3t73ATB7h27efIg/viewform?usp=sf_link")

input_text = browser.find_element(By.CSS_SELECTOR, "div input")
input_text.click()
browser.implicitly_wait(5)
input_text.send_keys("testing 123")

enter = browser.find_element(By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[3]/div[1]/div[1]/div')
enter.click()

browser.quit()

when i run the code , it shows:

 Traceback (most recent call last):
 File "/home/XXX/XXX.py", line 16, in <module> 
 input_text.send_keys("testing 123")
 selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

please help~~ i have been stuck for an hour! :(

It may be related to the fact the browser on PythonAnywhere runs in a headless mode, so you need to provide some extra parameters (like window-size) so it could calculate where to click. Have a look at this Stack Overflow answer.

thanks a lot! pafk, i changed my code to:

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--window-size=1920x1080")

browser = webdriver.Chrome(options=chrome_options)

try:   
     browser.get("https://docs.google.com/forms/d/e/1FAIpQLSfKIJCTUbSzYZmvFqY6jRS0RVvRQZZ_nkL3t73ATB7h27efIg/viewform?usp=sf_link")`

    WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'whsOnd'))).send_keys("testing 123")

finally:
    browser.quit()

now another error pops up.............. again the code i tried to run on pycharm and it works fine, i tried different method(e.g By.XPATH, By.CSS_SELECTOR) and still encountered TimeoutException...omg am sad now

Traceback (most recent call last):
File "/home/Ericlau/XXXX.py", line 18, in <module>
WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.CLASS_NAME, 'whsOnd'))).send_keys("testing 123")
File "/usr/local/lib/python3.9/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

OK, so it sounds like the element never became clickable. Perhaps you could use the browser's screenshot functionality to see what the page looks like? The code would be something like this:

browser.get_screenshot_as_file("/home/Ericlau/screenshot.png")

...and you could put it in your finally clause, just before the browser.quit()

giles, thanks for your reply!

enter image description here

it is something to do with the window size? i have tried to add driver.set_window_size(1920, 1080) driver.maximize_window()

below the broswer.get(url) still donest worked for me

I don't think it's the window size, no.

Which element is the one that you're trying to locate with the EC.element_to_be_clickable((By.CLASS_NAME, 'whsOnd'))? Is it the "your name" text entry field?

enter image description here

it is supposed to look like this , so it is because the element is changed, no wonder i cant get the correct element to paste the text i want....but how am i able to find it since i can only get the screenshot.......

You can grab all the page into file and then check how to identify it, maybe some regex, maybe something specific about parent element. It's hard to say form the screenshot.