我是 Selenium 的新手,这是我出现的错误
我的代码:
def job_search(self):
"""This function goes to the 'Jobs' section and looks for all the jobs that match the keywords and location"""
# Go to the LinkedIn job search page
self.driver.get("https://www.linkedin.com/jobs")
# Wait for the job search page to load fully
WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.CSS_SELECTOR, ".jobs-search-box__text-input[aria-label='Search by title, skill, or company']")))
# Search based on keywords and location
search_keywords = self.driver.find_element(By.CSS_SELECTOR, ".jobs-search-box__text-input[aria-label='City, state, or zip code']")
search_keywords.clear()
search_keywords.send_keys(self.keywords)
# Wait for the search location input field to be interactable
search_location = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jobs-search-box__input--location")))
search_location.clear()
search_location.send_keys(self.location)
search_location.send_keys(Keys.RETURN)
这是我遇到的错误
ElementNotInteractableException: element not interactable
(Session info: chrome=114.0.5735.134)
我想与 LinkedIN 中的“搜索栏”进行交互,但不幸的是错误欢迎了我
你们非常接近。您用于识别搜索框的定位器策略并不能唯一地识别搜索框。相反,它标识了总共 3 元素。
要定位 clickable 元素并发送字符序列而不是 presence_of_element_ located(),您需要为 element_to_be_clickable() 引入 WebDriverWait,并且可以使用以下任一 定位器策略:
使用CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.jobs-search-box__text-input.jobs-search-box__keyboard-text-input[aria-label='Search by title, skill, or company'][aria-activedescendant]"))).send_keys(self.keywords)
使用XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='jobs-search-box__text-input jobs-search-box__keyboard-text-input' and @aria-label='Search by title, skill, or company'][@aria-activedescendant]"))).send_keys(self.keywords)
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC