Search code examples
pythonseleniumxpathcss-selectorswebdriverwait

element not interactable: [object SVGGElement] has no size and location error interacting with search field on YouTube homepage using Selenium Python


Code trials:

from selenium import webdriver
from selenium.webdriver import Keys, ActionChains
from selenium.webdriver.chrome.service import Service
from time import time
from selenium.webdriver.common.actions.action_builder import ActionBuilder
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


service = Service(executable_path="C:\\Users\\aps\\Desktop\\Python\\chromedriver.exe")
driver = webdriver.Chrome(service=service)
driver.get("https://youtube.com")


search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "search")))

ActionChains(driver).move_to_element(search).pause(2).click_and_hold().send_keys("Iktarfa").perform()
button = driver.find_element(By.ID, "search-icon-legacy").click()

Search field is getting fetched.enter image description here

But after that I am getting following error, I am out of ideas and a new learner. Please HELP!!

enter image description here


Solution

  • The locator strategy which you have used:

    (By.ID, "search")
    

    identifies multiple elements within the HTML DOM.

    youtube_search

    and the first WebElement having the property style="display: none;". Hence you see the error.


    Solution

    To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

    • Using CSS_SELECTOR:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "ytd-searchbox#search"))).send_keys("text")
      
    • Using XPATH:

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytd-searchbox[@id='search']"))).send_keys("text")
      
    • Note: You have to add the following imports :

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC