2016-08-16 103 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() 

对于人们处理类似问题来完成,我会建议点击空间的边缘,让您选择并检查那里的元素。这可以防止您意外地使用最内层标签之一。