Python Selenium发现了错误的元素,该元素不';甚至不存在

Python Selenium发现了错误的元素,该元素不';甚至不存在,python,selenium,web,web-scraping,Python,Selenium,Web,Web Scraping,我正在检查firefox中的youtube搜索栏并获得: <input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Search" aria-label="Search" aria-haspopup="false" role="combobo

我正在检查firefox中的youtube搜索栏并获得:

<input id="search" autocapitalize="none" autocomplete="off" autocorrect="off" name="search_query" tabindex="0" type="text" spellcheck="false" placeholder="Search" aria-label="Search" aria-haspopup="false" role="combobox" aria-autocomplete="list" dir="ltr" style="outline: currentcolor none medium;" class="ytd-searchbox">
并获得以下错误(使用firefox gecko webdriver):

引发异常\u类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.ElementNotInteractiableException:消息:键盘无法访问元素

为什么??当我检查源代码时,没有id=“search”的g元素,所以发生了什么事?

elementnotinteractitableexception在找到元素时发生,但您无法与它交互

原因有很多:

  • 元素不可见/不显示
  • 元素在屏幕外
  • 元素位于另一个元素后面或隐藏
要解决您的问题,您需要使用actionchain,请参阅以下解决方案:

请尝试以下代码:

url = 'http://www.youtube.com'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)

element = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='search-form']//div[@id='container']//div[@id='search-input']//input")))

actionChains = ActionChains(driver)
actionChains.move_to_element(element).click().perform()
actionChains.move_to_element(element).send_keys("Bollywood",Keys.RETURN).perform()
注意:请将以下导入添加到您的解决方案中

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
输出:


谢谢,但EC和By是什么?你知道为什么我的代码不起作用吗?努力学习。啊,妈的,对不起。是的,这很有效。你知道为什么我的代码不起作用吗?我试着在我的代码中等待10秒,但它不起作用。请您回答,然后从您的末端点击向上投票按钮。
url = 'http://www.youtube.com'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)

element = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@id='search-form']//div[@id='container']//div[@id='search-input']//input")))

actionChains = ActionChains(driver)
actionChains.move_to_element(element).click().perform()
actionChains.move_to_element(element).send_keys("Bollywood",Keys.RETURN).perform()
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait