selenium-webdriverselenium-chromedriver

I'm getting an "element not interactable" error in selenium, how do I fix it?


I'm new to Selenium. I am trying to interact with the search bar of this website: https://www.careerjunction.co.za, in order to have the user of my program search for some job, and then scrape the information about that job. This is my code so far:

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

link = 'https://www.careerjunction.co.za'
path = r"C:\Users\--myName\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe"
service = Service(executable_path=path)
driver = webdriver.Chrome(service = service)

driver.get(link)
time.sleep(2)

u_search = input("Please enter the job you are looking for: ")
search = driver.find_element(By.NAME, "keywords")
search.send_keys(u_search)
search.send_keys(Keys.RETURN)

I am getting an element not interactable error, I assume on the search bar element. I have tried:

search = WebDriverWait(driver, 10).until(
    EC.element_to_be_clickable((By.NAME, "keywords"))

As well as:

driver.execute_script("arguments[0].scrollIntoView(true);", search)

Also using:

version 137.0.7151.69 Chrome and 137.0.7151.68 ChromeDriver

Any insights as to what the problem may be? But neither seem to work. Any insights to what the issue may be?


Solution

  • Issue:

    By.NAME, "keywords"
    

    Above code is the issue. If you inspect the HTML DOM and look carefully there are 2 elements with the attribute name=keywords. Your code will try to locate the first element which is a <meta> tag. And this is giving the ElementNotInteractbale exception.

    Solution: Use the ID locator strategy instead of NAME. See below:

    By.ID, "Keywords"
    

    Code: See the refactored working code below.

    1. I have removed time.sleep() and have instead used selenium's waits which are mode effective
    2. I have commented the code where chrome driver is manually added into the script. Instead have utilized Selenium Manager which will take care of driver management.
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.wait import WebDriverWait
    
    link = 'https://www.careerjunction.co.za'
    # path = r"C:\Users\--myName\Downloads\chromedriver-win64\chromedriver-win64\chromedriver.exe"
    # service = Service(executable_path=path)
    # driver = webdriver.Chrome(service = service)
    driver = webdriver.Chrome()
    driver.get(link)
    driver.maximize_window()
    wait = WebDriverWait(driver, 10)
    # Accept cookies
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn.btn-accept"))).click()
    
    u_search = input("Please enter the job you are looking for: ")
    search = wait.until(EC.element_to_be_clickable((By.ID, "Keywords")))
    search.send_keys(u_search)
    search.send_keys(Keys.RETURN)