ElementNotVisibleException:消息:尝试单击 youtube 搜索中的顶部视频时出现元素不可交互错误

ElementNotVisibleException: Message: element not interactable error while trying to click on the top video in a youtube search

我似乎无法找到一种方法来单击正确的元素以获得我正在寻找的 url。本质上,我试图在 youtube 搜索中点击 top 视频(排名最高的返回视频)。

How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver? -> 这是针对 Java 但它让我朝着正确的方向前进(知道我需要执行 Java 脚本)

http://www.teachmeselenium.com/2018/04/17/python-selenium-interacting-with-the-browser-executing-javascript-through-javascriptexecutor/ -> 这告诉我应该如何尝试在 python 中执行 javascript。

我也看过无数关于等待的文章,但都没有解决我的问题。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

wrds = ["Vivaldi four seasons", "The Beatles twist and shout", "50 
cent heat"] #Random list of songs

driver = webdriver.Chrome()

for i in wrds:
    driver.get("http://www.youtube.com")
    elem = driver.find_element_by_id("search")
    elem.send_keys(i)
    elem.send_keys(Keys.RETURN)

    time.sleep(5)
    driver.execute_script("arguments[0].click()",driver.find_element_by_id('video-title')) #THIS CLICKS ON WRONG VIDEO
    #elem = driver.find_element_by_id("video-title").click() #THIS FAILS
    time.sleep(5)

    url = driver.current_url

driver.close()

当我不执行任何 javascript 时,我得到一个 ElementNotVisibleException: Message: element not interactable 错误(即使它实际上已经工作了,但它远非健壮)。当我执行 javascript 时,它点击了错误的视频。

我已经尝试了所有类型的等待 "Explicit" 和 "Implicit" 这现在有效了。 我很确定我需要执行一些 Java 脚本,但我不知道如何执行。

这就是您永远不应使用 JavaScript 单击的原因之一,Selenium Webdriver 旨在刺激真实用户能够单击。真实用户无法单击页面中的不可见元素,但您可以单击 Javascript。如果您通过该 id video-title 搜索该元素,它总共匹配 53 个视频。但我不知道你想点击哪一个。您可以通过其他方式(而不是通过 id)匹配该元素。

我会给你一个如何点击那个元素的想法,但你需要在点击之前先找出索引。

driver.find_element_by_xpath("(//*[@id='video-title'])[1]").click

如果第一个不可见,则传递 2,[2] 然后传递三个,找出它点击所需元素的那个。或者您可以指定确切的元素,我们可能会尝试通过其他方式定位该元素。

你快到了。您需要引入 WebDriverWait 以使 元素可点击 并且您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wrds = ["Vivaldi four seasons", "The Beatles twist and shout", "50 cent heat"]
    kwrd = ["Vivaldi", "Beatles", "50"]
    options = webdriver.ChromeOptions()
    options.add_argument("start-maximized")
    options.add_argument("disable-infobars")
    options.add_argument("--disable-extensions")
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
    for i, j in zip(wrds, kwrd):
        driver.get("https://www.youtube.com/")
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search"))).send_keys(i)
        driver.find_element_by_css_selector("button.style-scope.ytd-searchbox#search-icon-legacy").click()
        WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "h3.title-and-badge.style-scope.ytd-video-renderer a"))).click()
        WebDriverWait(driver, 10).until(EC.title_contains(j))
        print(driver.current_url)
    driver.quit()