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
# 开启会话
driver = webdriver.Chrome()
driver.get("http://www.baidu.com")
# 全屏
driver.maximize_window()
driver.find_element_by_id('kw').send_keys('向往的生活')
driver.find_element_by_id('su').click()
# 显性等待
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//div[@tpl="tieba_general"]//a[text()="吧_百度贴吧"]')))
# 获取当前所有窗口
windows = driver.window_handles
print('目前所有的窗口:{}'.format(windows))
# 点击百度贴吧链接
driver.find_element_by_xpath('//div[@tpl="tieba_general"]//a[text()="吧_百度贴吧"]').click()
# 等待新窗口出现
WebDriverWait(driver,20).until(EC.new_window_is_opened(windows))
# 切换到最新打开的窗口
driver.switch_to.window(driver.window_handles[-1])
# 显性等待
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//a[@id="j_head_focus_btn"]')))
#点击加关注
driver.find_element_by_xpath('//a[@id="j_head_focus_btn"]').click()
WebDriverWait(driver,20).until(EC.visibility_of_element_located((By.XPATH,'//p[@id="TANGRAM__PSP_11__footerULoginBtn"]')))
print(driver.current_url)
driver.quit()
selenium窗口操作
最新推荐文章于 2024-06-05 15:21:34 发布