How to switch to new window in Selenium for Python?

后端 未结 5 1884
梦谈多话
梦谈多话 2020-11-22 13:16

I am working on selenium automation project using Python.

I am facing an issue, which is handling multiple browser windows.

Scenario is as f

5条回答
  •  旧时难觅i
    2020-11-22 13:49

    for eg. you may take

    driver.get('https://www.naukri.com/')
    

    since, it is a current window ,we can name it

    main_page = driver.current_window_handle
    

    if there are atleast 1 window popup except the current window,you may try this method and put if condition in break statement by hit n trial for the index

    for handle in driver.window_handles:
        if handle != main_page:
            print(handle)
            login_page = handle
            break
    
    driver.switch_to.window(login_page)
    

    Now ,whatever the credentials you have to apply,provide after it is loggen in. Window will disappear, but you have to come to main page window and you are done

    driver.switch_to.window(main_page)
    sleep(10)
    

提交回复
热议问题