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

1
  • See here. Commented Mar 5, 2017 at 11:37

3 Answers 3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

thanks alot for your help i will not forget it just i added time.sleep(5) and it works
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)

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.