我正在尝试删除此网站:
我最终希望得到所有问题/答案并正确答案
因此,这是我的代码,它将通过所有Q/A和正确的答案使我进入测验结束
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from webdriver_manager.chrome import ChromeDriverManager
#import time
import json
import pandas as pd
driver = webdriver.Chrome('C:/Users/Ihnhn/Documents/WebScrap/Selenium/chromedriver.exe')
driver.get("http://scrumquiz.org/#/scrum-master-practice-test") #démarre la page
driver.maximize_window()#met en full screen
#démarre le quizz
start_quizz = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[class='btn btn-primary btn-quiz-start']"))).click()
driver.execute_script("window.scrollTo(0,400);") #scroll jusqu'en bas de la question
for i in range(40):
next_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[class='btn btn-primary']"))).click()
complete_quizz = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Complete quiz')]"))).click()
,然后尝试使用此代码单击每个问题,获取所有信息,并回到测验结果页面,但这总是回到第一个问题。因此,它给了我40次相同的问题。
这是一个列表,但总是让我成为第一个元素?
(我刚刚试图暂时获取问题名称)
all_questions = driver.find_elements_by_xpath("//div[@class='quiz-answer wrong-answer']")
driver.execute_script("window.scrollTo(0,3000);") #scroll jusqu'en bas de la question
for q in all_questions:
click_question = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='quiz-answer wrong-answer']"))).click()
#time.sleep(2)
driver.execute_script("window.scrollTo(0,400);") #scroll jusqu'en bas de la question
nom_question = driver.find_element_by_xpath("//div[contains(@class,'question-title text-center')]/h3").text
print(nom_question)
#time.sleep(2)
back_question = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Back to the results')]"))).click()
我尝试在元素中替换“驱动程序”,以单击但无法使用。
Im trying to scrap this website : http://scrumquiz.org/#/scrum-master-practice-test
I want in the end to get all the questions/answers and correct answers
So here's my code which will get me to the end of the quizz with all the Q/A and correct answers
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
from webdriver_manager.chrome import ChromeDriverManager
#import time
import json
import pandas as pd
driver = webdriver.Chrome('C:/Users/Ihnhn/Documents/WebScrap/Selenium/chromedriver.exe')
driver.get("http://scrumquiz.org/#/scrum-master-practice-test") #démarre la page
driver.maximize_window()#met en full screen
#démarre le quizz
start_quizz = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[class='btn btn-primary btn-quiz-start']"))).click()
driver.execute_script("window.scrollTo(0,400);") #scroll jusqu'en bas de la question
for i in range(40):
next_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button[class='btn btn-primary']"))).click()
complete_quizz = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Complete quiz')]"))).click()
then, Im trying with this code to click on each question, get all the informations needed and get back to the Quiz result page, but it always go back to the first question. So it gives me 40 times same question.
it is a list but always gets me the first element ?
(I have just tried to get the question name for now)
all_questions = driver.find_elements_by_xpath("//div[@class='quiz-answer wrong-answer']")
driver.execute_script("window.scrollTo(0,3000);") #scroll jusqu'en bas de la question
for q in all_questions:
click_question = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//div[@class='quiz-answer wrong-answer']"))).click()
#time.sleep(2)
driver.execute_script("window.scrollTo(0,400);") #scroll jusqu'en bas de la question
nom_question = driver.find_element_by_xpath("//div[contains(@class,'question-title text-center')]/h3").text
print(nom_question)
#time.sleep(2)
back_question = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"//button[contains(.,'Back to the results')]"))).click()
I've tried to replace "driver" by "q" in the elements to be clickable but doesnt work.
发布评论
评论(1)
好吧,这可能是 xy问题的情况。
我知道这不是您要求的,而是无论如何:
通过查看网络请求浏览器在访问页面后发送(F12 - > Network)在
您可以看到一个用于
/scrum-Master.json
。因此,通过访问 scrum-master.json 您可以下载文件并解析。 (例如,使用Python的 json模块)。
看来
分辨率
字段包含正确答案的索引。始终是顶级选项,或者如果是多项选择,则始终是前2名。答案的顺序可能在客户端JS中被散装。
Well, this might be a case of an XY Problem.
I know it's not what you asked for, but anyway:
By looking at the network requests the browser sends after visiting the page, (f12 -> Network)
you can see one for
/scrum-master.json
.So, by visiting scrum-master.json you can download the file and parse it. (for example, with python's json module).
it seems the
resolution
field contains the indices of the correct answer(s). always the top option, or if it's a multiple choice, top 2.The order of answers is probably shuffled in the client-side JS.