【发布时间】:2019-02-06 16:22:00
【问题描述】:
当我运行我的脚本时,它在完成while 循环中的任务之前就结束了。
driver = webdriver.Chrome()
driver.get('http://example.com')
#input("Press any key to continue1")
s_b_c_status = "False"
while s_b_c_status == "True":
try:
if(driver.find_element_by_xpath("//div[@role='button' and @title='Status']")):
s_b_c_status = "True"
except NoSuchElementException:
s_b_c_status = "False"
if(s_b_c_status == "True"):
print("Scanning Done!")
else:
print("Error")
由于我的网站没有该元素,它应该总是打印Error,但是当我运行我的代码时,它只打印一次Error(尽管它在while 循环中进行了检查)。
我到底需要什么: 脚本应该检查元素是否存在,直到元素存在,然后运行其余代码。
【问题讨论】:
-
元素出现通常需要等待多长时间?
-
10-20秒之间,我不想在这里使用
time.sleep()... -
@Rabe 我认为 usecase 中存在一些混淆,正如您提到的
my site is not having the element it should always print Error但后来您提到了check whether the element is there or not till the element is there,所以主要问题是该元素是那里?如果是这样,您对元素的下一个粗略操作是什么?解决方案将取决于该条件。 -
元素100%肯定会存在,但它会出现多少时间后并不固定,因为它会根据用户的互联网速度...
-
@Rabe 好的,您对元素的下一个粗略操作是什么? 检索某些属性(例如text)或调用click()?
标签: python-3.x loops selenium while-loop