7、切换、创建窗口或标签页


一、切换窗口或标签页

from seleniumimport webdriver

from selenium.webdriver.support.uiimport WebDriverWait

from selenium.webdriver.supportimport expected_conditionsas EC

from selenium.webdriver.chrome.optionsimport Options

# 启动驱动程序options_cha = Options()

options_cha.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

chrome_driver ="/usr/local/bin/chromedriver"driver = webdriver.Chrome(chrome_driver, options=options_cha)

driver.get("https://seleniumhq.github.io")

# 设置等待

wait = WebDriverWait(driver, 10)

print("1")

# 存储原始窗口的 

IDoriginal_window = driver.current_window_handle

print("2")

# 检查一下,我们还没有打开其他的窗口(目前打开了两个窗口)

assert len(driver.window_handles) ==2print("3")

# 单击在新窗口中打开的链接

driver.find_element_by_link_text("Downloads").click()

print("4")

# 等待新窗口或标签页                                          

wait.until(EC.number_of_windows_to_be(2))

print("5")

# 循环执行,直到找到一个新的窗口句柄

for window_handlein driver.window_handles:

if window_handle != original_window:

driver.switch_to.window(window_handle)

print("no")

breakprint("6")

# 等待新标签页完成加载内容

wait.until(EC.title_is("SeleniumHQ Browser Automation"))

print("7")


二、创建新窗口(或)新标签页并且切换

from seleniumimport webdriver

from selenium.webdriver.support.uiimport WebDriverWait

from selenium.webdriver.supportimport expected_conditionsas EC

from selenium.webdriver.chrome.optionsimport Options# 启动驱动程序options_cha = Options()

import time


options_cha = Options()

options_cha.add_experimental_option("debuggerAddress", "127.0.0.1:9222")

chrome_driver ="/usr/local/bin/chromedriver"driver = webdriver.Chrome(chrome_driver, options=options_cha)

#打开百度,执行代码

driver.execute_script("window.open('https://seleniumhq.github.io')")

original_window = driver.current_window_handle

print("a ",driver.current_url)

print("b ",driver.window_handles)

print("c ",original_window)

driver.switch_to.window(driver.window_handles[1])

time.sleep(3)

driver.switch_to.window(original_window)


三、close和quit

1、当你完成了浏览器会话,你应该调用 quit 退出,而不是 close 关闭:

driver.quit() 

driver.close()

退出将会:

  关闭所有与 WebDriver 会话相关的窗口和选项卡

  结束浏览器进程

  结束后台驱动进程

  通知 Selenium Grid 浏览器不再使用,以便可以由另一个会话使用它(如果您正在使用 Selenium Grid)


2、测试框架

def tearDown(self): 

       self.driver.quit()

3、try / finally

try:

    #WebDriver 代码…

finally:

    driver.quit()


四、Frames and Iframes

# driver.switch_to.frame("frame1") #使用ID

driver.switch_to.frame(0) #使用索引

driver.find_element_by_id('kw').send_keys("abc")

driver.switch_to.default_content() #离开框架


五、窗口管理

# 分别获取每个尺寸

width = driver.get_window_size().get("width")

height = driver.get_window_size().get("height")

print("width : ",width," height : ",height)

# 或者存储尺寸并在以后查询它们

size = driver.get_window_size()

width1 = size.get("width")

height1 = size.get("height")

print("width1 : ",width1," height1 : ",height1)


#设置窗口大小

driver.set_window_size(1400, 668)

width2 = driver.get_window_size().get("width")

height2 = driver.get_window_size().get("height")

print("width2 : ",width2," height2 : ",height2)


#获取浏览器窗口左上角的坐标。

# 分别获取每个尺寸

x = driver.get_window_position().get('x')

y = driver.get_window_position().get('y')

print("x : ",x," y : ",y)

# 或者存储尺寸并在以后查询它们position = driver.get_window_position()

x1 = position.get('x')

y1 = position.get('y')

print("x1 : ",x1," y1 : ",y1)


# 将窗口移动到主显示器的左上角

driver.set_window_position(0, 50)

x = driver.get_window_position().get('x')

y = driver.get_window_position().get('y')

print("x : ",x," y : ",y)


driver.maximize_window()

time.sleep(2)

driver.minimize_window()

time.sleep(2)

driver.fullscreen_window()

你可能感兴趣的:(7、切换、创建窗口或标签页)