ElementNotInteractableException: Selenium

huangapple go评论68阅读模式
英文:

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(&quot;https://www.linkedin.com/jobs&quot;)

    # Wait for the job search page to load fully
    WebDriverWait(self.driver, 15).until(EC.presence_of_element_located((By.CSS_SELECTOR, &quot;.jobs-search-box__text-input[aria-label=&#39;Search by title, skill, or company&#39;]&quot;)))

    # Search based on keywords and location
    search_keywords = self.driver.find_element(By.CSS_SELECTOR, &quot;.jobs-search-box__text-input[aria-label=&#39;City, state, or zip code&#39;]&quot;)
    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, &quot;.jobs-search-box__input--location&quot;)))
    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 &quot;Search bar&quot; 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, &quot;input.jobs-search-box__text-input.jobs-search-box__keyboard-text-input[aria-label=&#39;Search by title, skill, or company&#39;][aria-activedescendant]&quot;))).send_keys(self.keywords)

- 使用_XPATH_:

	  WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//input[@class=&#39;jobs-search-box__text-input jobs-search-box__keyboard-text-input&#39; and @aria-label=&#39;Search by title, skill, or company&#39;][@aria-activedescendant]&quot;))).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&#39;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, &quot;input.jobs-search-box__text-input.jobs-search-box__keyboard-text-input[aria-label=&#39;Search by title, skill, or company&#39;][aria-activedescendant]&quot;))).send_keys(self.keywords)

- Using _XPATH_:
 	
	  WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, &quot;//input[@class=&#39;jobs-search-box__text-input jobs-search-box__keyboard-text-input&#39; and @aria-label=&#39;Search by title, skill, or company&#39;][@aria-activedescendant]&quot;))).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>



huangapple
  • 本文由 发表于 2023年6月22日 00:23:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525361.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定