2013-08-24 38 views
4

我想點擊Selenium中的Google首頁上的Gmail鏈接和Python上的WebDriver。我的代碼基本上覆制了一個在這裏找到:Why Cant I Click an Element in Selenium?selenium python點擊元素什麼也沒有發生

我的代碼:

import selenium.webdriver as webdriver 
firefox = webdriver.Firefox() 
firefox.get("http://www.google.ca") 
element = firefox.find_element_by_xpath(".//a[@id='gb_23']") 
element.click() 

的webdriver的加載頁面,然後什麼也沒有發生。我嘗試過使用ActionChains和move_to_element(element),單擊(element),然後執行(),但沒有任何反應。

+0

xpath is從來沒有一件好事與硒一起使用。嘗試嚴格按id搜索,因爲你有它 – Greg

+0

該代碼適用於我..你可以測試相同的代碼,但使用鉻而不是Firefox? – alecxe

+0

你可以嘗試降級Firefox。我讀過新版本的Firefox經常會破壞Selenium的交互。 – Richard

回答

5

使用find_element_by_id方法:

element = firefox.find_element_by_id("gb_23") 
element.click() 

或糾正你的XPath:

"//a[@id='gb_23']" 

Here you have nice tutorial.

+0

我剛剛嘗試運行這兩個選項。什麼都沒有發生...... –

+0

嗯,奇怪...試試吧:'firefox.click(「xpath = // a [@ id ='gb_23']」)' – swietyy

+0

我降級到了20版本,它工作。謝謝。 –

0

試試這個,因爲我不能看到HTML這個ID:

driver = webdriver.Firefox() 
driver.get("http://www.google.ca") 
element = driver.find_element_by_link_text("Gmail") 
element.click() 
相關問題