2

我的框架中有一个问题,我尝试等待元素的可见性,而不是使用静态睡眠。问题是元素的可见性检查 DOM 上元素的存在,这将返回 true,但在我的系统中,页面尚未完全加载。发生的情况是,一旦我在检查元素的可见性时得到了正确的设置值。当实际页面完全加载时,这些值会被重置。

我的问题是我可以使用什么代替静态睡眠来等待实际页面(不仅是 DOM)完全加载,因为元素的可见性对我不起作用?

PS我正在使用带有python 2.7的Selenium webdriver

/亚当

4

2 回答 2

4

expected_conditions.visibility_of_element_located(locator)方法将检查两者 - DOM 中元素的存在,以及它的可见性(元素以大于零的高度和宽度显示)。

理想情况下,该driver.get(url)方法应该自动等待整个页面加载完毕,然后再继续下一行。但是,如果正在测试的 Web 应用程序使用 ajax 调用/操作(虽然页面已加载但 ajax 操作仍在进行中),这可能不会按预期运行。在这种情况下,我们可以使用类似下面的方法来等待稳定性,然后再对所需的 web 元素执行操作。

# create the firefox driver
driver = webdriver.Firefox()

# navigate to the app url
driver.get('http://www.google.com')

# keep a watch on jQuery 'active' attribute
WebDriverWait(driver, 10).until(lambda s: s.execute_script("return jQuery.active == 0"))

# page should be stable enough now, and we can perform desired actions
elem = WebDriverWait(driver, 10).until(expected_conditions.visibility_of_element_located((By.ID, 'id')))

elem.send_keys('some text')

希望这可以帮助..

于 2015-12-13T20:58:38.997 回答
1

试试ExpectedConditions.elementToBeClickable。见:https ://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#elementToBeClickable-org.openqa.selenium.By-

于 2013-04-26T19:01:41.733 回答