Selenium/Python浏览器循环

2024-10-04 09:26:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

browser.implicitly_wait(10)

Tags: 代码divbrowser脚本sendidby情况
2条回答

这取决于你在等什么。在

下面是一个使用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

您可以在因特网上找到关于python循环的文档。在

下面是一个示例:

好好读书!在

相关问题 更多 >