2

I am new to Selenium so am making a bot to auto-complete a trivia so I can understand this module and it's good practice. Sometimes the .click() function does not work.

I have tried:

  • Using JavaScript to click
  • Using WebDriverWait
  • quitting and closing the driver and re opening page
  • Keep on clicking until a checked box id is valid (keeps on looping)
  • Refreshing the page and trying to click again (Java and normal .click()
  • using time.sleep(time) to wait instead
  • not using webdriver wait at all and just setting the var

I did used to have a stale element error, but I fixed that.

Note: It clicks most of the time, but randomly just doesn't work, not error or anything

Imports:

from selenium import webdriver
from time import *
from data import *
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, StaleElementReferenceException
from winsound import *
import os

Part of code:

def AutoAnswer(self, answers_list, link):
    global i
    if i == 1:
        self.driver.get(link)
    # sends browser to first trivia
    # sets the variable to the current question so that the correct answer can be located through answer lists

self.current_question = (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[2]')))).text

#setting variables for boxes and their text values. The text can be corrolated to the button allowing the bot to click the correct answer
#/html//div[@id="quizContainer"]/div[@class="answersContainer"]/div[1]/span[@class="answerBox"]/a[@name="checkboxtag"]

for i in range(0, len(answers_list)):
    if answers_list[i]["question"] == self.current_question:
        self.current_answer = answers_list[i]["answer"]


self.xpath_1  = '//*[@id="quizContainer"]/div[3]/div[1]/span[1]/a'
self.text_1   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[1]/span[2]')))).text


self.xpath_2  = '//*[@id="quizContainer"]/div[3]/div[2]/span[1]/a'
self.text_2   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[2]/span[2]')))).text


self.xpath_3  = '//*[@id="quizContainer"]/div[3]/div[3]/span[1]/a'
self.text_3   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[3]/span[2]')))).text


self.xpath_4  = '//*[@id="quizContainer"]/div[3]/div[4]/span[1]/a'
self.text_4   = ((WebDriverWait(self.driver, 120)).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="quizContainer"]/div[3]/div[4]/span[2]')))).text

while True:
    if self.text_1 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_1)))).click()
    if self.text_2 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_2)))).click()
    if self.text_3 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_3)))).click()
    if self.text_4 == self.current_answer:
        (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_4)))).click()

    if bot.driver.find_elements_by_class_name("largecheckboxselected") != []:
        print("all good")
        break
    else:
        print("problemo re-running")
        if self.text_1 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_1)))).click()
        if self.text_2 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_2)))).click()
        if self.text_3 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_3)))).click()
        if self.text_4 == self.current_answer:
            (WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH, self.xpath_4)))).click()
# selects and clicks the "Next Question!" button
nextquestion_btn = WebDriverWait(self.driver, 120).until(EC.element_to_be_clickable((By.XPATH,('//*[@id="nextQuestion"]'))))
nextquestion_btn.click()

Running the code:

bot = TriviaBot()
for i in range(1, 13):
    print("Run: "+str(i))
    bot.AutoAnswer(american_presidents, "https://www.freekigames.com/american-presidents-trivia")
bot.claimRewards()

the data.py

username = "username"
password = "password"
american_presidents = [
    {"question" : "Who was the 1st president of the United States?",
     "answer"   : "George Washington"},
    {"question" : "Who was the 2nd president of the United States?",
     "answer"   : "John Adams"},
    {"question" : "Who was the 3rd president of the United States?",
     "answer"   : "Thomas Jefferson"},
    {"question" : "Who was the 4th president of the United States?",
     "answer"   : "James Madison"},
    {"question" : "Who was the 5th president of the United States?",
     "answer"   : "James Monroe"},
    {"question" : "Who was the 6th president of the United States?",
     "answer"   : "John Quincy Adams"},
    {"question" : "Who was the 7th president of the United States?",
     "answer"   : "Andrew Jackson"},
    {"question" : "Who was the 8th president of the United States?",
     "answer"   : "Martin Van Buren"},
    {"question" : "Who was the 9th president of the United States?",
     "answer"   : "William Henry Harrison"},
    {"question" : "Who was the 10th president of the United States?",
     "answer"   : "John Tyler"},
    {"question" : "Who was the 11th president of the United States?",
     "answer"   : "James K. Polk"},
    {"question" : "Who was the 12th president of the United States?",
     "answer"   : "Zachary Taylor"},
    {"question" : "Who was the 13th president of the United States?",
     "answer"   : "Millard Fillmore"},
    {"question" : "Who was the 14th president of the United States?",
     "answer"   : "Franklin Pierce"},
    {"question" : "Who was the 15th president of the United States?",
     "answer"   : "James Buchanan"},
    {"question" : "Who was the 16th president of the United States?",
     "answer"   : "Abraham Lincoln"},
    {"question" : "Who was the 17th president of the United States?",
     "answer"   : "Andrew Johnson"},
    {"question" : "Who was the 18th president of the United States?",
     "answer"   : "Ulysses S. Grant"},
    {"question" : "Who was the 19th president of the United States?",
     "answer"   : "Rutherford B. Hayes"},
    {"question" : "Who was the 20th president of the United States?",
     "answer"   : "James A. Garfield"},
    {"question" : "Who was the 21st president of the United States?",
     "answer"   : "Chester A. Arthur"},
    {"question" : "Who was the 22nd president of the United States?",
     "answer"   : "Grover Cleveland"},
    {"question" : "Who was the 23rd president of the United States?",
     "answer"   : "Benjamin Harrison"},
    {"question" : "Who was the 24th president of the United States?",
     "answer"   : "Grover Cleveland"},
    {"question" : "Who was the 25th president of the United States?",
     "answer"   : "William McKinley"},
    {"question" : "Who was the 26th president of the United States?",
     "answer"   : "Theodore Roosevelt"},
    {"question" : "Who was the 27th president of the United States?",
     "answer"   : "William Howard Taft"},
    {"question" : "Who was the 28th president of the United States?",
     "answer"   : "Woodrow Wilson"},
    {"question" : "Who was the 29th president of the United States?",
     "answer"   : "Warren G. Harding"},
    {"question" : "Who was the 30th president of the United States?",
     "answer"   : "Calvin Coolidge"},
    {"question" : "Who was the 31st president of the United States?",
     "answer"   : "Herbert Hoover"},
    {"question" : "Who was the 32nd president of the United States?",
     "answer"   : "Franklin D. Roosevelt"},
    {"question" : "Who was the 33rd president of the United States?",
     "answer"   : "Harry S. Truman"},
    {"question" : "Who was the 34th president of the United States?",
     "answer"   : "Dwight D. Eisenhower"},
    {"question" : "Who was the 35th president of the United States?",
     "answer"   : "John F. Kennedy"},
    {"question" : "Who was the 36th president of the United States?",
     "answer"   : "Lyndon B. Johnson"},
    {"question" : "Who was the 37th president of the United States?",
     "answer"   : "Richard Nixon"},
    {"question" : "Who was the 38th president of the United States?",
     "answer"   : "Gerald Ford"},
    {"question" : "Who was the 39th president of the United States?",
     "answer"   : "Jimmy Carter"},
    {"question" : "Who was the 40th president of the United States?",
     "answer"   : "Ronald Reagan"},
    {"question" : "Who was the 41st president of the United States?",
     "answer"   : "George H. W. Bush"},
    {"question" : "Who was the 42nd president of the United States?",
     "answer"   : "Bill Clinton"},
    {"question" : "Who was the 43rd president of the United States?",
     "answer"   : "George W. Bush"},
    {"question" : "Who was the 44th president of the United States?",
     "answer"   : "Barack Obama"}

]

chemical_elements = [
    {"question" : "Most of the earth's atmosphere consists of this gas.",
     "answer"   : "N"},
    {"question" : "The symbol 'Au' refers to which chemical element?",
     "answer"   : "Gold"},
    {"question" : "The symbol 'Co' refers to which chemical element?",
     "answer"   : "Cobalt"},
    {"question" : "The symbol 'Cu' refers to which chemical element?",
     "answer"   : "Copper"},
    {"question" : "The symbol 'F' refers to which chemical element?",
     "answer"   : "Fluorine"},
    {"question" : "The symbol 'H' refers to which chemical element?",
     "answer"   : "Hydrogen"},
    {"question" : "The symbol 'Pb' refers to which chemical element",
     "answer"   : "Lead"},
    {"question" : "The symbol 'S' refers to which chemical element?",
     "answer"   : "Sulfur"},
    {"question" : "This element give plants the energy they need to grow.",
     "answer"   : "P"},
    {"question" : "This element is the building block of life.",
     "answer"   : "C"},
    {"question" : "This element was discovered by Hans Christian Oersted in 1825.",
     "answer"   : "Al"},
    {"question" : "This element was discovered by Joseph Priestly and Carl Scheele in 1774.",
     "answer"   : "O"},
    {"question" : "This element was discovered in 1808.",
     "answer"   : "B"},
    {"question" : "This element when combined with Chlorine makes table salt.",
     "answer"   : "Na"},
    {"question" : "What is the symbol for Potassium?",
     "answer"   : "K"},
    {"question" : "Which element has a reddish color in a gas and liquid state?",
     "answer"   : "Br"},
    {"question" : "Which element has a silver-gray appearance?",
     "answer"   : "Zn"},
    {"question" : "Which of these elements is considered a Metal.",
     "answer"   : "Fe"},
    {"question" : "Which of these elements is NOT considered a Metalloid.",
     "answer"   : "Sn"},
    {"question" : "Which of these elements is NOT considered a Noble Gas.",
     "answer"   : "H"}

]

I have seen some previous answers, they were to do with the stale element error which is solved. I believe this is a different issue.

6
  • It might be a syncranization issue. Try putting some waits before the check. Commented Feb 6, 2020 at 19:28
  • Could you elaborate. What kind of wait, and what check. What line should I insert or name the check you are talking about. Thanks Commented Feb 6, 2020 at 19:57
  • Could you provide checkbox HTML code? Commented Feb 7, 2020 at 8:59
  • <a name="checkboxtag" class="largecheckbox" onclick="selectQuizAnswer(this);"></a> Commented Feb 7, 2020 at 16:30
  • When checked: <a name="checkboxtag" class="largecheckboxselected" onclick="selectQuizAnswer(this);"></a> Commented Feb 7, 2020 at 16:32

2 Answers 2

1

I am extremely sorry. I was focused on my code rather than anything else, I failed to notice that it always failed on the "chemical elements trivia". I also failed to notice that it always failed on one question; this was because I had forgot to put in a word in the data.py dictionary questions. It is all working now.

Thank you all for your help. And once again I apologize

I feel so dumb right now :/

Sign up to request clarification or add additional context in comments.

1 Comment

Let me point another problem you might find out, some websites that construct the DOM tree dynamically will give you issues if you don't wait a bit before clicking. For example, you want to click a button that appears after doing a certain action. You do the action, and wait for the button to appear then click, however, the click event might not have been bound yet (you're racing against the website scripting) and the click would be fired but do nothing, then fail in the next action.
0

Three more ideas:

  1. Fluent Wait:

    Wait<WebDriver> fluentWait = new FluentWait<>(driver) .withTimeout(30, TimeUnit.SECONDS) .pollingEvery(300, TimeUnit.MILLISECONDS) .ignoring(NoSuchElementException.class) .ignoring(StaleElementReferenceException.class);

    public WebElement fluentWait(By by) { WebElement waitElement = null; try { waitElement = fluentWait.until(webDriver -> getDriver().findElement(by)); } catch (TimeoutException ex) {} return waitElement; }

  2. Use actions class

    WebElement element = fluentWait(by); Actions actions = new Actions(getDriver()); actions.moveToElement(element); actions.click(); actions.build().perform();

  3. Figure out which events you should be waiting for. Is the application written in Angular? React? JQuery?

https://www.swtestacademy.com/selenium-wait-javascript-angular-ajax/

1 Comment

Thanks for the reply, but is this Java or JavaScript? I am using Python. If this is python then I have no idea how to use it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.