python关闭windows窗口_Windows处理会关闭整个浏览器,如果我尝试关闭python中的当前窗口...

Am currently using windows handling for opening the map direction in the new window and after it opens i will be closing the child window, which is opened and do the remaming work in the code.But it is closing the whole browser, while debugging it is working correctly , but while running the code, am Getting the error as,

Error - selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed

Down i have attached the code,

##Clicking on The Map Image

self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()

##Setting up an Window Handle to get the size.

handels =self.driver.window_handles

size = len(handels)

"""

The Below For Loop, We are using For Handling The Mutilple Windows,

Which are opened in the Browser.

"""

for length in range(size):

driver.switch_to.window(handels[length])

print(self.driver.title)

time.sleep(3)

if length == 1:

driver.close()

Where i have done the error i dunno. please sort me out.

解决方案

Try this code to close new window and switch back to main window

from selenium.webdriver.support.ui import WebDriverWait

from selenium.webdriver.support import expected_conditions as EC

# Get current window

current = self.driver.current_window_handle

# Get current windows

handles = self.driver.window_handles

# Click button. Consider to use more reliable relative XPath instead of this absolute XPath

self.driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[2]/div[1]/section[1]/div[1]/div[1]/div[2]/div[1]/a[1]/img[1]").click()

# Wait for new window

WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(handles))

# Switch to new window

self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])

# Close new window

self.driver.close()

# Switch back to main window

self.driver.switch_to.window(current)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值