1

I'm trying to create a bot for Quizlet for fun, but I've encountered this problem for "Match." Essentially, the program should drag and drop tiles to other tiles in order to match them all and make them disappear. All the elements are already correctly ordered in the html (so I just need to match the indexes 0-1, 2-3, 4-5, 6-7... in the list). Every time it matches two elements, those two disappear from the DOM, so I'm guessing that's the issue.

Here is my current code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
from selenium.common import exceptions
import time

driver = webdriver.Firefox()
actionChains = ActionChains(driver)


def match():
    driver.find_element(By.LINK_TEXT, 'Match').click()
    driver.find_element(By.CLASS_NAME, 'UIButton.UIButton--hero').click()
    for _ in range(6):
        a = driver.find_elements(By.CLASS_NAME, 'MatchModeQuestionScatterTile')
        actionChains.drag_and_drop(a[0], a[1]).perform()


def main():
    driver.get('https://quizlet.com/397790434/random-flash-cards/')
    match()


if __name__ == '__main__':
    main()

Every time I loop, I find all the elements again, so I don't understand why they are stale.

I also tried repeatedly looping the drag and drop, though it just never ends:

def match():
    driver.find_element(By.LINK_TEXT, 'Match').click()
    driver.find_element(By.CLASS_NAME, 'UIButton.UIButton--hero').click()
    for i in range(6):
        a = driver.find_elements(By.CLASS_NAME, 'MatchModeQuestionScatterTile')
        while True:
            try:
                actionChains.drag_and_drop(a[2 * i], a[2 * i + 1]).perform()
                break
            except exceptions.StaleElementReferenceException:
                pass

I've been stuck for a long while, so any help or ideas would be appreciated. Thank you!

Just a brief update/bump: I also attempted to move them by coordinates, though that also did not work consistently (stuck after 1-2 loops). After checking the html source, I notice that the class adds a new attribute for translation units, though I'm not sure if that changes anything. I also tried deleting parts of the list that wouldn't be present in the html after being matched, though that did not help either. Not sure if it is something specific about Quizlet, or if I'm missing something conceptually.

I'm pretty much completely out of ideas now, so any ideas would be greatly appreciated. Thank you!

the41
  • 11
  • 3

0 Answers0