简体   繁体   中英

webscraping selenium : my loop always get only the 1st element

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM