selenium 浏览器标签切换

from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()
#options.add_argument('--headless')
br = webdriver.Chrome(chrome_options=options)
br.get("http://www.spbeen.com")

abutton = br.find_element_by_xpath('//*[@id="content_show"]/div/div[1]/div[1]/div[1]/h3/a')
print(abutton.get_attribute('href'))

new_table_js = 'window.open("{}")'.format(abutton.get_attribute('href'))
br.execute_script(new_table_js)

print(br.window_handles)
for i in range(0,2):
    br.switch_to.window(br.window_handles[i])

print(br.current_window_handle)
br.close()
br.switch_to.window(br.window_handles[0])

sleep(6)
br.quit()

 

posted @ 2020-03-12 10:34  brady-wang  阅读(378)  评论(0编辑  收藏  举报