我希望能够执行类似于WebDriverWait()
, i.e:
WebDriverWait(driver, 60).until(
expected_conditions.text_to_be_present_in_element((By.XPATH, "//tr[5]/td[11]/div"), "1.000000")
)
...对于正则表达式,它在失败之前等待指定的时间。我知道我可以做一些事情,比如……
assert re.search(r"[0,1]{1}.[0-9]{6}", driver.find_element_by_xpath("//tr[5]/td[11]/div").text)
...或者我可以替换search with match在上面的例子中。此方法的问题是,如果对象.. (1) 尚未加载或.. (2) 仍在更改为预期内容的过程中,它将失败。我可以做类似的事情...
for x in range (1,60):
try:
assert re.search(r"[0,1]{1}.[0-9]{6}", driver.find_element_by_xpath("//tr[5]/td[11]/div").text)
except AssertionError:
if x < 60:
time.sleep(1)
else:
raise AssertionError
...它每秒检查 60 秒,看看是否assert语句已评估为 true。这可以适合模块或类。我想知道是否有一个更优雅的解决方案,在 Python 中用于 Selenium WebDriver,来处理我不知道的这个问题。