[笔记]Selenium Testing Tools Cookbook_Chapter6

Chapter 6 Working with Alerts, Frames, and Windows

6.1 Handling a simple JavaScript alert box

import time

driver.execute_script("window.alert('This is an Alert box');")
time.sleep(5)

alert = Alert(driver)
alert.accept()

6.1.1 The NoAlertPresentException

The alert() throws a NoAlertPresentException exception when it tries to access an alert box that doesn't exist

6.2 Handling a confirm and prompt alert box

6.2.1 Handling a confirm box

button = driver.find_element_by_xpath("//input[@value='Confirm Alert Box']")
button.click()

alert = Alert(driver)
alert.dismiss()
message = driver.find_element_by_xpath("//font")
tc.assertEqual("Not Confirm",message.text)

6.2.2 Handling a prompt box

button = driver.find_element_by_xpath("//input[@value='Prompt Alert Box']")
button.click()

alert = Alert(driver)
alert.send_keys("1")
alert.accept()
message = driver.find_element_by_xpath("//font")
tc.assertEqual("You input 1",message.text)

6.3 Identifying and handling frames

driver.switch_to.frame("left")
driver.switch_to.default_content()

6.4 Working with IFRAME

jianFrame = driver.find_element_by_tag_name("iframe")
driver.switch_to.frame("jianFrame")

6.5 Identifying and hangling a child window

wechat = driver.find_element_by_id("weixin")
wechat.click()
time.sleep(5)
driver.switch_to.window(driver.window_handles[1])
print(driver.title)

6.6 Identifying and handling a window by its title

wechat = driver.find_element_by_id("weixin")
wechat.click()
time.sleep(5)

try:
    for windowID in driver.window_handles:
        driver.switch_to.window(windowID)
        title = driver.title
        if title == "微信登录":
            tc.assertEqual("微信登录",driver.title)
            driver.close()
finally:
    driver.switch_to.window(window_handles[0])

6.7 Identifying and handling a pop-up window by its content

CurrentWindow = driver.current_window_handle

button = driver.find_element_by_id("sign_in")
button.click()
time.sleep(5)

wechat = driver.find_element_by_id("weixin")
wechat.click()
time.sleep(5)

try:
    for windowID in driver.window_handles:
        driver.switch_to.window(windowID)
        PageSource = driver.page_source
        if "二维码登录" in PageSource:
            print(driver.title)
            driver.close()
finally:
    driver.switch_to.window(CurrentWindow)

你可能感兴趣的:([笔记]Selenium Testing Tools Cookbook_Chapter6)