0

Novice trying to figure things out. I've looked around for an answer and not found one.
While trying to interact with a webpage, I get this message from Python:

element not interactable

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.annemcgilvray.com")

search = driver.find_element_by_name("q")

search.send_keys("test")

I've tried waiting and implicitly waiting. I don't think it is in an iframe, though there are iframes on the page.

Any help would be appreciated!

2 Answers 2

1

When I tried your code, the problem was the the site did not adjust to the size of my browser window and the search field was out of view, and hence could not be interacted with, although selenium could locate it.

I tried using execute_script to scroll it into view, and after that, send_keys worked.

from selenium import webdriver

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

driver.get("https://www.annemcgilvray.com")

search = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

driver.execute_script(f"window.scrollBy({search.location['x']},0)")

search.send_keys("test")
Sign up to request clarification or add additional context in comments.

Comments

0

You need to add wait / delay to let the element fully loaded before accessing it.
The best approach is to use explicit wait implemented by expected conditions, as following:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
wait = WebDriverWait(driver, 20)

driver.get("https://www.annemcgilvray.com")

search = wait.until(EC.visibility_of_element_located((By.NAME, "q")))

search.send_keys("test")

2 Comments

Thank you! This worked!
If so please accept my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.