Home > Blockchain >  How to make a click on button with selenium?
How to make a click on button with selenium?

Time:07-02

I'm writing a code with selenium. My code should open YouTube, input a word, click on search button and open a video. Everything except the last one. I can't open a video. Could you please help me?

My code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[@id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[@id="search-icon-legacy"]')
searchbutton.click()
elements = driver.find_element(By.XPATH, '//*[@id="video-title"]/yt-formatted-string').click()

CodePudding user response:

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

driver = webdriver.Chrome('/Users/mariabiriulina/Desktop/chromedriver')
wait = WebDriverWait(driver, 20)

driver.get('https://youtube.com/')
searchbox = driver.find_element(By.XPATH, '//input[@id="search"]')
searchbox.click()
searchbox.send_keys('Justin Timberlake')
searchbutton = driver.find_element(By.XPATH, '//*[@id="search-icon-legacy"]')
searchbutton.click()

video_titles_xpath = "//*[@id='video-title']/yt-formatted-string"
wait.until(EC.visibility_of_element_located((By.XPATH, video_titles_xpath)))
video_titles = driver.find_elements(By.XPATH, video_titles_xpath)
video_titles[0].click()
  • Related