Selenium / Python浏览器循环

时间:2015-11-27 15:30:18

标签: python selenium selenium-webdriver selenium-chromedriver

我有一个用Python编写的基本脚本来检查div并单击一个按钮。我使用browser.implicitly_wait自动重新加载但是如何让它循环遍历代码而不重复它?

elem = browser.find_element_by_id('sitediv'); elem.send_keys('Red' + Keys.RETURN);

browser.implicitly_wait(10)

2 个答案:

答案 0 :(得分:0)

你可以在互联网上找到有关python循环的文档。

以下是一个示例:

读得很好!

答案 1 :(得分:0)

这取决于你还在等什么。

这是一个示例,等待一个元素出现在具有while_is_visible函数的页面上,以检查该元素是否存在于页面上:

class DockerContainerDeployer(object):

    self.browser = webdriver.Firefox()

    def deploy(self)
        # actions...
        # wait until the docker container is deployed 
        # by checking a div that give us the status
        while not self._is_visible("#cluster-status .deployed"):
            # you can add an implicity wait to check it every 10s with
            # self.browser.implicitly_wait(10)
            pass
        # docker container deployed

    def _is_visible(self, locator, timeout = 2):
        """
        Check if an element is visible
        """

        try:
            ui.WebDriverWait(browser, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, locator)))
            return True
        except TimeoutException:
            return False