简体   繁体   English

Python使用Selenium切换窗口

[英]Python switching windows with Selenium

Could you help me with an advice? 你能帮我个建议吗? I'm doing some QA test and am testing a site. 我正在做一些质量检查测试,并且正在测试一个网站。 During the process, one by one windows in Chrome open and I'm trying to switch between these windows in Chrome. 在此过程中,Chrome中一个一个打开的窗口,我正在尝试在Chrome中切换这些窗口。 I don't succeed to switch to the third window. 我没有成功切换到第三个窗口。 Can you help please? 你能帮忙吗? I'm using Pycharm and Selenium framework. 我正在使用Pycharm和Selenium框架。

Here is the part of the Code where I ask your assistance: 这是《守则》中我需要您帮助的部分:

second_login= webdriver.Ie()
second_login.maximize_window()
second_login.get('http://google.com')
print(second_login.title)
second_login.find_element_by_id('id1').click()
tab_now = second_login.window_handles[1]
second_login.switch_to.window(tab_now)
print(second_login.title)
log = second_login.find_element_by_id('id2')
log.send_keys('admin1')
pas=second_login.find_element_by_id('id3')
pas.send_keys('pas1')
logbutton=second_login.find_element_by_class_name('LoginButton')
logbutton.click()
second_login.implicitly_wait(3)
tab_after = second_login.window_handles[-1]
second_login.switch_to.window(tab_after)
second_login.find_element_by_id('id4').click()
print(second_login.title)

Thank you. 谢谢。

As described here , you can do it by using window_handles and switch_to_window method. 如上所述在这里 ,您可以通过使用window_handles和switch_to_window方法做到这一点。

Before clicking the link first window handle as 在单击链接之前,第一个窗口的句柄为

window_before = driver.window_handles[0]

Second window handle of newly opened window as 新打开的窗口的第二个窗口句柄为

window_after = driver.window_handles[1]

I assume the third window would be 我认为第三个窗口是

driver.window_nandles[2]

Also take a look here , you can loop trough windows, just break on the 3rd window and do whatever you want: 还可以在这里一下 ,您可以循环通过槽形窗口,只需在第三个窗口上断开就可以做任何想做的事情:

for handle in driver.window_handles:
    driver.switch_to_window(handle)

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM