1

I am trying to click using class to video on youtube

In this page https://likesrock.com using this code

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://likesrock.com")

element = driver.find_element_by_class_name("ytp-large-play-button ytp-button")
element.click()

but the compilor give me this error

Traceback (most recent call last): File "C:\Users\youssef\Desktop\python project\xpath.py", line 25, in element = driver.find_element_by_class_name("ytp-large-play-button ytp-button") File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 413, in find_element_by_class_name return self.find_element(by=By.CLASS_NAME, value=name) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element 'value': value})['value'] File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: Unable to locate element: .ytp-large-play-button ytp-button

please help

Tri
  • 369
  • 1
  • 4
  • 13
jozef
  • 13
  • 1
  • 3

3 Answers3

2

You are using compound classes in find_element_by_class_name which won't work. Change it to find_element_by_xpath and try

driver.find_element_by_xpath("//*[@class='ytp-large-play-button ytp-button']")

Note :- Your element is under iFrame so don't forgot to switch in iframe

driver.switch_to.frame(0) 

Complete Code :

driver = webdriver.Firefox()
driver.get("https://likesrock.com")
driver.implicitly_wait(20) 
driver.switch_to.frame(0) 
element = driver.find_element_by_xpath("//button[@class='ytp-large-play-button ytp-button']")
element.click()
NarendraR
  • 7,577
  • 10
  • 44
  • 82
1
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome("chromedriver.exe")
driver.get('https://youtube.com')

search_bar = driver.find_element_by_xpath('/html/body/ytd-app/div/div/ytd-masthead/div[3]/div[2]/ytd-searchbox/form/div/div[1]/input')
search_bar.send_keys("python")
search_bar.send_keys(Keys.ENTER)
0

I was just attempting to play the video by launching Youtube website.

Code provided by @Narendra worked well with Chrome but couldn't work with Firefox.

driver.get(url)
element = driver.find_element_by_id("player-container")
element.click()

Above code worked with Firefox as well.

rajat
  • 1
  • 1