Python While循环中的动态变量

2024-06-02 22:43:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了这个python循环代码。你知道吗

  1. 如果实际点数小于或等于9,则执行任务A
  2. 否则,如果实际点数大于9,则执行任务B
  3. 执行While循环,直到实际点大于9

这是我的代码

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

问题:

上面的代码工作不正常,因为变量actualpoints是动态的。你知道吗

如果actualpoints<;9,它将执行分配的任务B,但不幸的是,它返回相同的变量,并且从不更改。你知道吗

任务A,重新加载页面并显示一个新的数字,该数字应存储在名为actualpoints的变量中。你知道吗

与我的代码和变量相关的其他详细信息:

  • strpoints=获取包含数字的字符串。这个字符串的一部分是静态文本和动态(数字)。例如:你的下列行为将得到12分。
  • 点=切片strpoints。你知道吗
  • actualpoints=切片strpoints后的结果。动态值。你知道吗
  • 将执行循环直到>;10

知道密码有什么问题吗?你知道吗


Tags: 代码nametaskbydriver动态数字element
2条回答

在下面的代码中,time.sleep替换为waitwhile替换为for循环。每次迭代都使用更新的值。用于从strpoints中提取points数字的正则表达式。你知道吗

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

我不确定这是否能解决问题,但也许您可以添加一个actualpoints验证方法和变量来保存最后一个actualpoints值?你知道吗

这是你的代码和我做的一些补充。如果我正确地阅读了任务A,我将您的初始过程修改为while循环,但是可以根据您的需要随意修改。你知道吗

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

相关问题 更多 >