Ben Daggers
Ben Daggers

Reputation: 992

Dynamic Variable in Python While Loop

I got this python selenium while loop code.

  1. If actual points less than or equal to 9 then do Tasks A
  2. Else if actual points greater than 9 do Task B
  3. Perform a While Loop until the actual points is greater than 9

Here's my Code

strpoints = driver.find_element_by_class_name("fs18").text
points = slice(13, len(strpoints)-20)
actualpoints = strpoints[points]

d = 0


while (d + actualpoints <9):
    # TASK A!!!
    print(actualpoints + ' Points! Skipping.')
    time.sleep(2)
    driver.find_element_by_class_name('skip_button_single').click()

    time.sleep(8)

    if (d >= 10):
        break
# TASK B!!!
print(actualpoints + ' Points! Go for it!')

Issue:

The code above is not working properly because the variable actualpoints is dynamic.

IF actualpoints < 9 it will performed the assigned TASK B BUT UNFORTUNATELY it returns the same variable and it never changes.

Task A, reloads the page and displays a new number that should be stored in a variable called actualpoints.

Other details related to my code and variables:

any idea what's wrong with the code?

Upvotes: 0

Views: 925

Answers (2)

Sers
Sers

Reputation: 12255

In code below, time.sleep replaced with wait and while with for loop. Each iteration getting strpoints to use updated value. Regular expression used to extract points number from strpoints.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import re

#...

wait = WebDriverWait(driver, 10)

for i in range(10):
    str_points = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "fs18"))).text
    print("str_points: " + str_points)
    points = re.search("\\d+", str_points)[0]

    if int(points) > 9:
        break

    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "skip_button_single"))).click()
    //time.sleep(8)

print(f'{points} Points! Go for it!')

Upvotes: 1

Mark Moretto
Mark Moretto

Reputation: 2348

I'm not sure if this will solve the problem, but maybe you can add an actualpoints validation method and variable to hold the last actualpoints value?

Here's your code and some of the additions I made. I reworked your initial process into the while loop if I'm reading TASK A correctly, but feel free to modify this to suit your needs.

strpoints = driver.find_element_by_class_name("fs18").text
points = slice(13, len(strpoints)-20)
actualpoints = strpoints[points]

"""
    Create a temporary variable equal to the initial actualpoints value
"""
old_actualpoints = actualpoints

d = 0

def validate_actualpoints():
    """
        Simple value check query. Returns actual actualpoints value.
    """
    if old_actualpoints != actualpoints:
        old_actualpoints = actualpoints

    return actualpoints


while old_actualpoints == actualpoints:
    while (d + actualpoints < 9):
        # TASK A!!!
        print(actualpoints + ' Points! Skipping.')
        time.sleep(2)
        driver.find_element_by_class_name('skip_button_single').click()

        """ Move the initial process into the while loop and re-run based on TASK A """
        strpoints = driver.find_element_by_class_name("fs18").text
        points = slice(13, len(strpoints)-20)
        actualpoints = strpoints[points]

        time.sleep(8)

        if (d >= 10):
            break

    """
    Update our temporary variable here?
    (Possibly not needed.)
    """
    old_actualpoints = validate_actualpoints()
    break
    # TASK B!!!
print(actualpoints + ' Points! Go for it!')

Upvotes: 1

Related Questions