Selenium with Python-无限期等待,直到出现一个输入框



我希望WebDriver实例无限期地监视页面,直到出现名称为"move"的输入框输入框出现后,我想用一些文本填充它,然后单击表单旁边的提交按钮。最简单的方法是什么?

我现在有这样的东西:

try:
    move = WebDriverWait(driver, 1000).until(
        EC.presence_of_element_located((By.NAME, "move"))
    )
finally:
    wd.quit()

表单旁边的按钮没有名称或id,所以我通过XPATH来定位它。我想等到表格出现后再点击按钮。

我该怎么做?

无限期地监视页面,直到出现输入框

您在示例中使用的显式等待需要定义的超时值。要么您设置了一个非常高的超时值,要么它不是一个选项。

或者,您可以有一个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元素是表单的后续同级。

相关内容

最新更新