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!
CodePudding user response:
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")
CodePudding user response:
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")