python - 单击youtube按钮 Selenium python检查我的代码

标签 python python-2.7 selenium selenium-webdriver youtube

我试图点击使用类在youtube上进行视频

在此页面中https://likesrock.com使用此代码

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()

但是编译器给我这个错误

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



请帮忙

最佳答案

您正在find_element_by_class_name中使用复合类,但无法使用。将其更改为find_element_by_xpath并尝试

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

注意:-您的元素位于iFrame下,因此不要忘记切换iframe
driver.switch_to.frame(0) 

完整的代码:
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()

关于python - 单击youtube按钮 Selenium python检查我的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42607714/

相关文章:

python - 比较两列以在 Spark DataFrame 中创建新列

python - Django: href {% url %} 问题

mysql - 结合日期和时间来创建时间序列的索引

python - 单击带有 selenium 和 python 的 href 按钮?

python - Matplotlib 中的 3D 参数化曲线不遵循 zorder。解决方法?

python - 确定 scipy.optimize 的合理初始猜测的函数?

python - 如何让 Sprite 随机生成?

python - 将 string.decode ('utf8' ) 从 python2 转换为 python3

selenium - 如何通过传递复选框名称来选择复选框 - 使用 Selenium/Webdriver

r - 无法通过 RStudio 的任务调度程序启动浏览器