2017-03-05 197 views
0

我試圖單擊使用類點擊YouTube上使用此代碼點擊YouTube的按鈕硒蟒蛇檢查我的代碼

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

但compilor給我這個erorr vadio

在此頁面https://likesrock.com

回溯(最近一次調用最後一次): 文件「C:\ Users \ youssef \ Desktop \ python project \ xpath.py」,第25行,在 element = driver.find_ele ment_by_class_name(「ytp-large-play-button ytp-button」) find_element_by_class_name中的第413行文件「C:\ Python27 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py」 return self.find_element (by = By.CLASS_NAME,value = name) 文件「C:\ Python27 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py」,第752行,在find_element中 'value':value})[ 'value'] 文件「C:\ Python27 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py」,第236行,執行 self.error_handler.check_response(響應) 文件「C:\ Python27 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py「,第192行,在check_response 引發exception_class(消息,屏幕,堆棧跟蹤) NoSuchElementException:消息:Unable to loca熱電元件:.ytp大型播放按鈕YTP-按鈕

+0

參見[這裏] (http://stackoverflow.com/q/10658907/7023590)。 – MarianD

回答

0

您正在使用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() 
+0

非常感謝您的幫助,我不會忘記它只是我添加time.sleep(5),它的工作原理 – jozef