0

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:

  • strpoints = getting the string that holds the number(s). Part of this string is static text and dynamic (numbers). Example: You will get 12 points for following.
  • points = slicing the strpoints.
  • actualpoints = the result after slicing the strpoints. Dynamic value.
  • will perform a loop until > 10

any idea what's wrong with the code?

3
  • Can you share strpoints value example Commented Mar 23, 2019 at 12:19
  • Should the starting code here also be in the while loop e.g. while True:? Otherwise you never execute code that has a chance to modify actualpoints. Commented Mar 23, 2019 at 12:32
  • Updated the answer with example of strpoints Commented Mar 23, 2019 at 12:38

2 Answers 2

1

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!')
Sign up to request clarification or add additional context in comments.

Comments

1

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!')

10 Comments

i got this error: TypeError: string indices must be integers. I tried this: actualpoints = int(strpoints[points]) but still the same error.
@BenDaggers use points = int(re.search("\\d+", strpoints)[0]). check answer update.
It (sort of) worked but the variable points is not correct. the actual point i tested was 14 but it only captured 4. I think there's an issue if the actual point is 2 digits. I have updated question showing an example of the strpoints variable.
another issue/error, if the actual point is 1 digit (ex. 5), it returns an error of ValueError: invalid literal for int() with base 10: 'i'
@BenDaggers try updated code and share print results
|

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.