ElementNotInteractableException: Selenium

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

ElementNotInteractableException: Selenium

问题

  1. 我是Selenium的新手,这里是我遇到的错误
  2. **我的代码:**
  3. ```python
  4. def job_search(self):
  5. """This function goes to the 'Jobs' section and looks for all the jobs that match the keywords and location"""
  6. # 前往LinkedIn职位搜索页面
  7. self.driver.get("https://www.linkedin.com/jobs")
  8. # 等待职位搜索页面完全加载
  9. 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']")))
  10. # 根据关键词和位置进行搜索
  11. search_keywords = self.driver.find_element(By.CSS_SELECTOR, ".jobs-search-box__text-input[aria-label='City, state, or zip code']")
  12. search_keywords.clear()
  13. search_keywords.send_keys(self.keywords)
  14. # 等待搜索位置输入字段可交互
  15. search_location = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".jobs-search-box__input--location")))
  16. search_location.clear()
  17. search_location.send_keys(self.location)
  18. search_location.send_keys(Keys.RETURN)

这是我遇到的错误

  1. ElementNotInteractableException: 元素不可交互
  2. (会话信息: chrome=114.0.5735.134)

我想与LinkedIN的“搜索栏”进行交互,但不幸的是,出现了这个错误

  1. <details>
  2. <summary>英文:</summary>
  3. I am new in Selenium, here is the error that i showed up
  4. **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"""

  1. # Go to the LinkedIn job search page
  2. self.driver.get(&quot;https://www.linkedin.com/jobs&quot;)
  3. # Wait for the job search page to load fully
  4. 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;)))
  5. # Search based on keywords and location
  6. 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;)
  7. search_keywords.clear()
  8. search_keywords.send_keys(self.keywords)
  9. # Wait for the search location input field to be interactable
  10. search_location = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, &quot;.jobs-search-box__input--location&quot;)))
  11. search_location.clear()
  12. search_location.send_keys(self.location)
  13. search_location.send_keys(Keys.RETURN)
  1. **Here is the error that i got**

ElementNotInteractableException: element not interactable
(Session info: chrome=114.0.5735.134)

  1. I want to interact with &quot;Search bar&quot; in LinkedIN, but unfortunately the error welcomed me
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 你的翻译请求已被记录,以下是翻译好的部分:
  6. ----------
  7. **Solution**
  8. --------
  9. 要定位**可点击的**元素并发送字符序列,而不是使用[_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):
  10. - 使用_CSS_SELECTOR_
  11. 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)
  12. - 使用_XPATH_
  13. 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)
  14. - **注意**:你需要添加以下导入:
  15. from selenium.webdriver.support.ui import WebDriverWait
  16. from selenium.webdriver.common.by import By
  17. from selenium.webdriver.support import expected_conditions as EC
  18. <details>
  19. <summary>英文:</summary>
  20. 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.
  21. ![3elements](https://i.stack.imgur.com/wRILy.png)
  22. ----------
  23. Solution
  24. --------
  25. 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):
  26. - Using _CSS_SELECTOR_:
  27. 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)
  28. - Using _XPATH_:
  29. 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)
  30. - **Note** : You have to add the following imports :
  31. from selenium.webdriver.support.ui import WebDriverWait
  32. from selenium.webdriver.common.by import By
  33. from selenium.webdriver.support import expected_conditions as EC
  34. </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:

确定