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()
原文地址:https://www.cnblogs.com/666666pingzi/p/10568884.html
时间: 2024-11-13 04:50:24