你快到了 您需要诱导 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 ECwrds = [“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:WebDriverschromedriver.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()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)