我需要检查打开的选项卡是否为空,然后切换到另一个选项卡。
我尝试了get_current_url()
方法,但是它不起作用。
def check_is_tab_empty(self, link):
self.click(link)
self.focus_active_tab()
tab = self.get_current_url()
此行tab = self.get_current_url()
-如果选项卡为空,例如about:blank
,则此行不起作用。
答案 0 :(得分:0)
您已切换到新标签页以获取新标签页的网址。此处以示例为例。您已打开空白页。
driver = webdriver.Chrome()
driver.get('https://www.yahoo.com')
windows_before = driver.current_window_handle
driver.execute_script('''window.open('{}');'''.format("about:blank"))
windows_after = driver.window_handles
new_window = [x for x in windows_after if x != windows_before][0]
driver.switch_to.window(new_window)
print(driver.current_url)
答案 1 :(得分:0)
我决定通过try ... except
语句来处理这种情况:
try:
tab = self.get_current_url()
except TimeoutException:
...
self.click(link)
self.focus_active_tab()
tab = self.get_current_url()
现在它对我有用,因为第二次打开链接(不是空链接)。
但是,如果有人知道更好的解决方案,请分享。