if else在Python上循环。使用Selenium检查类名 [英] if else loop on Python. Checking a class name with Selenium
问题描述
我有这个预约系统,我必须等到链接可用。如果此链接可用,则单击它。如果不回来和foward(因为页面不让我重新加载)。并再次检查,直到可用。
I have this appointment system where i have to wait till the link is available. If this link its availble then click it. If not back and foward (beacause the page doesn't let me reload ). And check it again till is available.
while True:
if driver.find_element_by_class_name("linkButton"):
#do somthing
else:
driver.back()
driver.forward()
#check again.
程序不会抛出任何错误但是当我强制 if
是 false
else
什么都不做。
The program doesn't throw any error but when i force the if
to be false
the else
just do nothing.
我无法使用不可用的链接检查它,因为在页面中可用,这就是为什么我强制如果
为 false
I can't check it with the link that is not available because in the page is till available that's why i force the if
to be false
推荐答案
首先, find_element _ *( )
方法不返回true / false,它们返回 WebElement
实例(这是真实的)或抛出 NoSuchElementException
异常(或其他异常)。
First of all, the find_element_*()
methods don't return true/false, they either return a WebElement
instance (which is truthy) or throw a NoSuchElementException
exception (or other exception).
存在性检查通常通过 find_elements _ *()$来完成c $ c>方法,如果找不到元素,将返回一个空列表,这是假的:
The existence check is usually done via find_elements_*()
methods which, if an element is not found, would return an empty list, which is falsy:
while True:
if driver.find_elements_by_class_name("linkButton"):
# do something
else:
driver.refresh()
请注意,我认为你只是想要 refresh()
页面而不是前后。
Note that I think you just meant to refresh()
the page instead of going back and forward.
并且,您还应该在尝试之间添加一些时间延迟。
And, you should also add some time delays between the attempts.
这篇关于if else在Python上循环。使用Selenium检查类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!