How can selenium web driver get to know when the new window has opened and then resume its execution

后端 未结 7 614
情深已故
情深已故 2020-12-03 07:05

I am facing an issue in automating a web application using selenium web driver.

The webpage has a button which when clicked opens a new window. When I use the follow

7条回答
  •  不知归路
    2020-12-03 07:34

    You could wait until the operation succeeds e.g., in Python:

    from selenium.common.exceptions    import NoSuchWindowException
    from selenium.webdriver.support.ui import WebDriverWait
    
    def found_window(name):
        def predicate(driver):
            try: driver.switch_to_window(name)
            except NoSuchWindowException:
                 return False
            else:
                 return True # found window
        return predicate
    
    driver.find_element_by_id("id of the button that opens new window").click()        
    WebDriverWait(driver, timeout=50).until(found_window("new window name"))
    WebDriverWait(driver, timeout=10).until( # wait until the button is available
        lambda x: x.find_element_by_id("id of button present on newly opened window"))\
        .click()
    

提交回复
热议问题