ElementNotInteractableException:消息:使用Selenium和ChromeDriver单击youtube搜索按钮时,元素不可交互错误

时间:2020-06-06 16:49:28

标签: python-3.x selenium-webdriver xpath youtube css-selectors

我正在使用最新的python和最新的Selenium chrome webdriver。我正在尝试使用一个简单的代码在youtube中进行搜索,但出现以下错误。谁能帮我吗?

File "search.py", line 8, in <module> searchBox.click()
Selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable

CodeStartsHere:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://youtube.com')

searchBox = driver.find_element_by_xpath('//*[@id="search"]')
if searchBox.is_enabled():
    searchBox.click()
    searchBox.send_keys("youtube test")
    searchButton = driver.find_element_by_xpath('//*[@id="search-icon-legacy"]/yt-icon')
    searchButton.click()
else:
    print("What the heck")
#CodeEndsHere

2 个答案:

答案 0 :(得分:0)

我在Youtube主页上找到了3个带有标签的元素,第一个元素不可见,第二个元素是您要访问的搜索字段。 使用此标签:

Get-WmiObject -ComputerName remotepc -Credential azuread\adminuser -Class Win32_BIOS

答案 1 :(得分:0)

要在https://youtube.com中发起搜索,您需要为element_to_be_clickable()引出WebDriverWait,并且可以使用以下Locator Strategies中的任何一个:

  • 使用CSS_SELECTOR

    driver.get('https://youtube.com')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='search_query']"))).send_keys("youtube test")
    driver.find_element_by_css_selector("button#search-icon-legacy>yt-icon").click()
    
  • 使用XPATH

    driver.get('https://youtube.com')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='search_query']"))).send_keys("youtube test")
    driver.find_element_by_xpath("//button[@id='search-icon-legacy']/yt-icon").click()
    
  • 注意:您必须添加以下导入:

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

浏览器快照:

youtube_search


参考

您可以在以下位置找到一些相关的讨论