wen自动化之浏览器的窗口切换
Posted 666666pingzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了wen自动化之浏览器的窗口切换相关的知识,希望对你有一定的参考价值。
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 driver = webdriver.Chrome() driver.get("https://www.baidu.com/") driver.maximize_window() window = driver.window_handles print("当前窗口:",driver.current_window_handle) driver.find_element_by_xpath(‘//input[@id="kw"]‘).send_keys("柠檬班") driver.find_element_by_xpath(‘//input[@id="su"]‘).submit() webelenment = (By.XPATH,‘//div[@tpl="tieba_general"]//a[text()="吧_百度贴吧"]‘) time.sleep(2) WebDriverWait(driver,20).until(EC.presence_of_element_located(webelenment)) driver.find_element_by_xpath(‘//div[@tpl="tieba_general"]//a[text()="吧_百度贴吧"]‘).click() # 判断窗口是否进行切换 WebDriverWait(driver,20).until(EC.new_window_is_opened(window)) # 进行窗口的切换 windows = driver.window_handles driver.switch_to.window(windows[-1]) print("切换后的窗口",driver.current_window_handle) # 点击签到操作 wb = (By.XPATH,‘//a[@title="签到"]‘) WebDriverWait(driver,20).until(EC.presence_of_element_located(wb)) time.sleep(2) driver.find_element_by_xpath(‘//a[@title="签到"]‘).click()
以上是关于wen自动化之浏览器的窗口切换的主要内容,如果未能解决你的问题,请参考以下文章