python-3.x - ElementNotVisibleException : Message: element not interactable error while trying to click on the top video in a youtube search

标签 python-3.x selenium selenium-webdriver youtube webdriverwait

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

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

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()

我得到一个 ElementNotVisibleException: Message: element not interactable当我不执行任何 javascript 时出现错误(即使它实际上已经工作了,但它几乎没有鲁棒性)。当我执行 javascript 时,它会点击错误的视频。

我已经尝试了所有类型的等待“显式”和“隐式”,这现在确实有效。
我很确定我需要执行一些 JavaScript,但我不知道如何。

最佳答案

你快到了。您需要诱导 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()
    
  • 关于python-3.x - ElementNotVisibleException : Message: element not interactable error while trying to click on the top video in a youtube search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54616023/

    相关文章:

    python - 从一个网页内的其他选项卡获取内容

    Python selenium 进入#document

    python - 如果我的 chromedriver 与 chrome 版本不兼容,我该如何修复它?

    c# - 使用 Selenium 使用 WindowHandles 跟踪和遍历选项卡和窗口的最佳方法

    python - 如何在 Selenium 中处理 Firefox 打印对话框

    python - 连续运行脚本 X 次(一次一个)的推荐方法是什么

    python - 按空格对单词进行标记 NLTK

    python - 在python中从键盘读取原始输入

    python - 将 csv 读取到 pandas 并按原样保留值

    python - 在列表中添加矩形以在pygame中以7段数字显示