2016-06-09 50 views
1

我開始學習使用Python和Selenium的Scrape網站。我選擇硒因爲我需要瀏覽網站,我也必須登錄。使用Python/Selenium的Webscrape Flashscore

我寫了一個腳本,能夠打開一個Firefox窗口,並打開網站www.flashscore.com。有了這個腳本,我也能夠登錄並導航到他們擁有的不同體育節(主菜單)。

代碼:


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

# open website 
driver = webdriver.Firefox() 
driver.get("http://www.flashscore.com") 

# login 
driver.find_element_by_id('signIn').click() 

username = driver.find_element_by_id("email") 
password = driver.find_element_by_id("passwd") 

username.send_keys("*****") 
password.send_keys("*****") 

driver.find_element_by_name("login").click() 

# go to the tennis section 
link = driver.find_element_by_link_text('Tennis') 
link.click() 

#go to the live games tab in the tennis section 

# ?????????????????????????????' 

然後就更加困難。例如,我還想要導航到體育領域的「實時遊戲」和「完成」選項卡。這部分不起作用。我嘗試了很多東西,但是我無法進入這個標籤。在分析網站時,我發現他們使用了一些Iframe。我也發現一些代碼切換到Iframes窗口。但問題是,我無法找到要點擊的選項卡的Iframe名稱。也許iframes不是問題,我看錯了方向。 (也許這個問題是由一些JavaScript引起的?)

任何人都可以請幫助我嗎?

回答

0

不,在這種情況下iframes不是問題。 「Live games」元素不在iframe之內。通過鏈接文本找到它,然後單擊:

live_games_link = driver.find_element_by_link_text("LIVE Games") 
live_games_link.click() 

您可能需要等待此鏈接,實際上是試圖單擊它之前可點擊

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

wait = WebDriverWait(driver, 10) 

live_games_link = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "LIVE Games"))) 
live_games_link.click() 
+0

的反應非常感謝。一個問題。運行我的腳本時出現錯誤。 – timovic

+0

在運行時這樣的:從進口硒webdriver的 從selenium.webdriver.common.keys匯入selenium.webdriver.support.ui導入密鑰 從selenium.webdriver.support進口expected_conditions爲EC 從selenium.webdriver.common WebDriverWait 。由進口到 #打開網站 司機= webdriver.Firefox() driver.get( 「http://www.flashscore.com」) #go在網球節 live_games_link =等待的比賽實況標籤.until(EC.element_to_be_clickable((By.LINK_TEXT,「LIVE Games」))) live_games_link.click() – timovic

+0

我得到錯誤:live_games_link = wait.until(EC.eleme nt_to_be_clickable((By.LINK_TEXT,「LIVE Games」))) NameError:name'wait'沒有定義 – timovic

相關問題