Selenium WebDriverWait页面加载如何写yield语句

问题描述 投票:0回答:1

我正在尝试等待一个页面完全加载Selenium,并尝试使用其他答案的代码:https://stackoverflow.com/a/30385843/8165689第三种方法在这个答案中使用Selenium的'staleness_of'属性,最初位于:http://www.obeythetestinggoat.com/how-to-get-selenium-to-wait-for-page-load-after-a-click.html

但是,我认为我在此代码中特别关注Python yield关键字。基于以上所述,我有方法:

@contextmanager
def wait_for_page_load(driver, timeout = 30):
    old_page = driver.find_element_by_tag_name('html')
    yield WebDriverWait(driver, timeout).until(staleness_of(old_page))

这不会被Python调用,断点显示它被跳过。我对明显的原始代码也有同样的问题:

@contextmanager
def wait_for_page_load(driver, timeout = 30):
    old_page = driver.find_element_by_tag_name('html') # up to here with decorator, the function is called OK, with 'yield' it is NOT called
    yield
    WebDriverWait(driver, timeout).until(staleness_of(old_page))

但是如果我之后删除了yield语句,那么这个函数至少会被调用:

@contextmanager
def wait_for_page_load(driver, timeout = 30):
    old_page = driver.find_element_by_tag_name('html')

谁知道我应该怎么写yield语句?我没有经验丰富,但看起来Python必须yield的东西,所以也许原始代码似乎有自己的产量有问题?

python selenium yield webdriverwait
1个回答
0
投票

我想你可能错过了这里的预期条件。请尝试该代码,看看这是否有帮助。

from selenium.webdriver.support import expected_conditions as EC
def wait_for_page_load(driver, timeout = 30):
    old_page = driver.find_element_by_tag_name('html') 
    yield WebDriverWait(driver, timeout).until(EC.staleness_of(old_page))
© www.soinside.com 2019 - 2024. All rights reserved.