英文:
ElementNotInteractableException: Selenium
问题
我是Selenium的新手,这里是我遇到的错误
**我的代码:**
```python
def job_search(self):
"""This function goes to the 'Jobs' section and looks for all the jobs that match the keywords and location"""
# 前往LinkedIn职位搜索页面
self.driver.get("https://www.linkedin.com/jobs")
# 等待职位搜索页面完全加载
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_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)
# 等待搜索位置输入字段可交互
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: 元素不可交互
(会话信息: chrome=114.0.5735.134)
我想与LinkedIN的“搜索栏”进行交互,但不幸的是,出现了这个错误
<details>
<summary>英文:</summary>
I am new in Selenium, here is the error that i showed up
**My code:**
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)
**Here is the error that i got**
ElementNotInteractableException: element not interactable
(Session info: chrome=114.0.5735.134)
I want to interact with "Search bar" in LinkedIN, but unfortunately the error welcomed me
</details>
# 答案1
**得分**: 0
你的翻译请求已被记录,以下是翻译好的部分:
----------
**Solution**
--------
要定位**可点击的**元素并发送字符序列,而不是使用[_presence_of_element_located()_](https://stackoverflow.com/a/57313803/7429447),你需要引入[WebDriverWait](https://stackoverflow.com/a/59130336/7429447)等待[_element_to_be_clickable()_](https://stackoverflow.com/a/54194511/7429447),然后你可以使用以下任一种[_locator strategies_](https://stackoverflow.com/a/48056120/7429447):
- 使用_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
<details>
<summary>英文:</summary>
You were pretty close. The [locator strategy](https://stackoverflow.com/questions/48369043/official-locator-strategies-for-the-webdriver) you have used to identify the search box, doesn't identifies the _Search Box_ uniquely. Rather it identifies total _**3**_ elements.
![3elements](https://i.stack.imgur.com/wRILy.png)
----------
Solution
--------
To locate the **clickable** element and send a character sequence instead of [_presence_of_element_located()_](https://stackoverflow.com/a/57313803/7429447)you need to induce [WebDriverWait](https://stackoverflow.com/a/59130336/7429447) for the [_element_to_be_clickable()_](https://stackoverflow.com/a/54194511/7429447) and you can use either of the following [_locator strategies_](https://stackoverflow.com/a/48056120/7429447):
- Using _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)
- Using _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)
- **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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论