ElementNotVisibleException:消息:尝试单击 YouTube 搜索中的顶部视频时元素不可交互错误 [英] ElementNotVisibleException: Message: element not interactable error while trying to click on the top video in a youtube search
问题描述
我似乎无法找到一种方法来单击正确的元素以获取我正在寻找的 url.本质上,我试图在 youtube 搜索中点击 top 视频(排名最高的返回视频).
I cannot seem to find a way to click on the right element in order to get the url I am looking for. In essence I am trying to click on the top video in a youtube search (the most highly ranked returned video).
如何解决ElementNotInteractableException:元素不可见在 Selenium 网络驱动程序中?-> 这是针对 Java 的,但它让我朝着正确的方向前进(知道我需要执行 JavaScript)
How to resolve ElementNotInteractableException: Element is not visible in Selenium webdriver? -> This is for Java but it let me in the right direction (knowing I needed to execute JavaScript)
http://www.teachmeselenium.com/2018/04/17/python-selenium-interacting-with-the-browser-executing-javascript-through-javascriptexecutor/-> 这向我展示了我应该如何尝试在 python 中执行 javascript.
http://www.teachmeselenium.com/2018/04/17/python-selenium-interacting-with-the-browser-executing-javascript-through-javascriptexecutor/ -> This shows me how I should try to execute the javascript in python.
我也看过无数关于等待的文章,但它们并没有解决我的问题.
I have also seen countless articles about waits but they do not solve my problem.
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 时,它会点击错误的视频.
I get a ElementNotVisibleException: Message: element not interactable
error when I do not execute any javascript (even though it has actually worked before it is just no way near robust). When I do execute the javascript it clicks on the wrong videos.
我已经尝试了所有类型的等待显式"和隐式",现在确实有效.我很确定我需要执行一些 JavaScript,但我不知道如何执行.
I have tried all types of waits "Explicit" and "Implicit" this did now work. I am quite sure I need to execute some JavaScript but I don't know how.
推荐答案
您就快到了.您需要引入 WebDriverWait 以使 元素可点击,您可以使用以下解决方案:
You were almost there. You need to induce WebDriverWait for the element to be clickable and you can use the following solution:
代码块:
Code Block:
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()
这篇关于ElementNotVisibleException:消息:尝试单击 YouTube 搜索中的顶部视频时元素不可交互错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!