2016-08-16 117 views
1

我正在努力使可汗學院上運行的自動Web刮板使用硒和python刮板進行離線備份(稍後再來)。我目前正致力於通過練習來回答問題(正確或錯誤無關緊要)。不幸的是,selenium的.click()函數實際上並沒有選擇一個答案。我認爲這與指出錯誤的對象有關,但我說不出來。它目前突出顯示該選項,但不選擇它。Selenium點擊但不選擇

HTML for a single option (out of 4)

我做了一些代碼來重現錯誤,並迷上它的測試賬號爲您所有的調試需求。謝謝。

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

driver = webdriver.Firefox() 

# gets us to the SAT Math Exercise page 
driver.get('https://www.khanacademy.org/mission/sat/tasks/5505307403747328') 

# these next lines just automate logging in. Nothing special. 
login = driver.find_element_by_name('identifier') 
login.send_keys('stackflowtest') # look, I made a new account just for you guys 
passw = driver.find_element_by_name('password') 
passw.send_keys('stackoverflow') 
button = driver.find_elements_by_xpath("//*[contains(text(), 'Sign in')]") 
button[1].click() 

driver.implicitly_wait(5) # wait for things to become visible 
radio = driver.find_element_by_class_name('perseus-radio-option') 
radio.click() 
check = driver.find_element_by_xpath("//*[contains(text(), 'Check answer')]") 
check.click() 
+0

如何處理一些html代碼? – JDelorean

+0

該元素具有類名「kui-button」爲什麼不使用這個代碼來查找元素driver.find_elements_by_classname(「kui-button」)]「) –

+0

@JDelorean HTML添加 – Xeneficus

回答

0

一個試錯的過程後,我發現實際點擊選擇可以通過指向與類「說明」的元素

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

driver = webdriver.Firefox() 

# gets us to the SAT Math Exercise page 
driver.get('https://www.khanacademy.org/mission/sat/tasks/5505307403747328') 

# these next lines just automate logging in. Nothing special. 
login = driver.find_element_by_name('identifier') 
login.send_keys('stackflowtest') # look, I made a new account just for you guys 
passw = driver.find_element_by_name('password') 
passw.send_keys('stackoverflow') 
button = driver.find_elements_by_xpath("//*[contains(text(), 'Sign in')]") 
button[1].click() 

driver.implicitly_wait(5) # wait for things to become visible 
radio = driver.find_element_by_class_name('description') 
radio.click() 
check = driver.find_element_by_xpath("//*[contains(text(), 'Check answer')]") 
check.click() 

對於人們處理類似問題來完成,我會建議點擊空間的邊緣,讓您選擇並檢查那裏的元素。這可以防止您意外地使用最內層標籤之一。