python-web自动化:windows窗口切换/iframe切换/alert弹窗切换


from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time


loc1 = "//input[@id='kw']"

driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
driver.maximize_window()
# 搜索腾讯课堂
loc1 = (By.XPATH, "//input[@id='kw']")
driver.find_element(*loc1).send_keys("腾讯课堂")

loc2 = (By.XPATH, '//input[@id="su"]')
driver.find_element(*loc2).click()
time.sleep(3)

# 进入腾讯课堂
loc3 = (By.XPATH, '//div[@id="1"]/h3/a/em[text()="腾讯课堂"]')
driver.find_element(*loc3).click()

# 获取打开的所有窗口
windows = driver.window_handles
print(windows)
# 切换到当前最新打开的窗口
driver.switch_to.window(windows[-1])


# 高级版切换窗口
#loc3 = (By.XPATH, '//div[@id="1"]/h3/a/em[text()="腾讯课堂"]')

# 获取打开的所有窗口
#windows = driver.window_handles

# 点击导致出现新的窗口
#driver.find_element(*loc3).click()

# 有新窗口了,重新获取一次窗口列表,再切换到最新窗口
#WebDriverWait(driver,10).until(EC.new_window_is_opened(windows))

# 获取打开的所有窗口
#windows = driver.window_handles

#print(windows)
# 切换到当前最新打开的窗口
#driver.switch_to.window(windows[-1])


# 点击登录按钮
time.sleep(2)
loc3 = (By.XPATH, '//a[text()="登录" and (@id="js_login")]')
driver.find_element(*loc3).click()

time.sleep(2)
loc4 = (By.XPATH, '//div[@class="content-btns"]/a[@data-type="1"]')
driver.find_element(*loc4).click()

time.sleep(2)

# 切换进入ifram 的三种方式
# 1.id/name属性值来定位
driver.switch_to.frame("login_frame_qq")

# 2.WebElement对象来定位
driver.switch_to.frame(driver.find_element_by_name("login_frame_qq"))

# 3.frameindex来定位
driver.switch_to.frame(3)

#高级版切换ifram
#WebDriverWait(driver,30).until(EC.frame_to_be_available_and_switch_to_it("login_frame_qq"))


loc5 = (By.XPATH,'//a[@id="switcher_plogin"]')
driver.find_element(*loc5).click()

posted on 2020-09-11 14:07  小小小、小鱼儿  阅读(371)  评论(0编辑  收藏  举报

导航