我在循环代码中得到了这个python selenium。
如果实际分数小于或等于9,则执行任务A
否则,如果实际点大于9,则执行任务B
执行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
是动态的。如果realpoints
任务A,重新加载页面并显示一个新数字,该数字应存储在名为
actualpoints
的变量中。与我的代码和变量有关的其他详细信息:
strpoints =获取包含数字的字符串。该字符串的一部分是静态文本和动态(数字)。示例:您将获得12分。
点=切片
strpoints
。Actualpoints =切片
strpoints
后的结果。动态值。将执行循环,直到> 10
知道代码有什么问题吗?
最佳答案
我不确定这是否可以解决问题,但是也许您可以添加一个Actualpoints验证方法和变量来保存最后的Actualpoints值?
这是您的代码以及我所做的一些补充。如果我正确阅读了TASK 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!')