我正在制作一个在Khan Academy上运行的自动网络刮刀,使用selenium和python scraper进行离线备份(稍后会发布)。我正在努力让它回答问题(对错是无关紧要的)继续练习。不幸的是,selenium的.click()函数实际上并没有选择答案。我认为这与指向错误的对象有关但我无法分辨。它目前突出显示该选项,但不选择它。
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 :(得分: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()
对于处理类似问题的人,我建议点击空间的边缘,让您选择并检查那里的元素。这可以防止您意外使用其中一个最里面的标签。