具有 Python 的 Selenium - 无限期等待直到出现输入框
Selenium with Python - Wait indefinitely until an input box is present
我希望 WebDriver 实例无限期地监视页面,直到出现名称为 'move.' 的输入框形式。最简单的方法是什么?
我现在有这样的东西:
try:
move = WebDriverWait(driver, 1000).until(
EC.presence_of_element_located((By.NAME, "move"))
)
finally:
wd.quit()
并且与表单相邻的按钮没有名称或 ID,所以我通过 XPATH 定位它。我想等到该表单出现后再单击按钮。
我该怎么做?
monitor a page indefinitely until an input box appears
您在示例中使用的 Explicit wait 需要定义超时值 。要么您为超时设置了一个非常高的值,要么它不是一个选项。
或者,您可以有一个 while True
循环,直到找到一个元素:
from selenium.common.exceptions import NoSuchElementException
while True:
try:
form = driver.find_element_by_name("move")
break
except NoSuchElementException:
continue
button = form.find_element_by_xpath("following-sibling::button")
button.click()
我假设 button
元素是 following sibling 形式的
我希望 WebDriver 实例无限期地监视页面,直到出现名称为 'move.' 的输入框形式。最简单的方法是什么?
我现在有这样的东西:
try:
move = WebDriverWait(driver, 1000).until(
EC.presence_of_element_located((By.NAME, "move"))
)
finally:
wd.quit()
并且与表单相邻的按钮没有名称或 ID,所以我通过 XPATH 定位它。我想等到该表单出现后再单击按钮。
我该怎么做?
monitor a page indefinitely until an input box appears
您在示例中使用的 Explicit wait 需要定义超时值 。要么您为超时设置了一个非常高的值,要么它不是一个选项。
或者,您可以有一个 while True
循环,直到找到一个元素:
from selenium.common.exceptions import NoSuchElementException
while True:
try:
form = driver.find_element_by_name("move")
break
except NoSuchElementException:
continue
button = form.find_element_by_xpath("following-sibling::button")
button.click()
我假设 button
元素是 following sibling 形式的