注册和搜索页面切换
'''
百度的注册和搜索页面切换
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
drive=webdriver.Chrome()
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
drive.get("http://www.baidu.com/")
search_windows=drive.current_window_handle
drive.find_element_by_link_text('登录').click()
WebDriverWait(drive,30,0.5).until(EC.presence_of_element_located((By.LINK_TEXT,"立即注册")))
drive.find_element_by_link_text('立即注册').click()
all_handles=drive.window_handles
for handle in all_handles:
if handle !=search_windows:
drive.switch_to.window(handle)
print('切换到了注册窗口')
drive.find_element_by_name('userName').send_keys('imcy')
drive.find_element_by_xpath("//*[@id='TANGRAM__PSP_3__password']").send_keys('chaoya0802')
time.sleep(2)
for handle in all_handles:
if handle ==search_windows:
drive.switch_to.window(handle)
print('切换到了搜索窗口')
drive.find_element_by_xpath("//*[@id='TANGRAM__PSP_4__closeBtn']").click()
drive.find_element_by_id("kw").send_keys("Selenium")
drive.find_element_by_id("su").click()
警告弹出处理
'''
百度的设置警告处理
'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
drive=webdriver.Chrome()
drive.get("http://www.baidu.com/")
link=drive.find_element_by_link_text('设置')
ActionChains(drive).move_to_element(link).perform()
drive.find_element_by_link_text('搜索设置').click()
WebDriverWait(drive,30,0.5).until(EC.presence_of_element_located((By.XPATH,"//*[@id='gxszButton']/a[1]")))
drive.find_element_by_xpath("//*[@id='gxszButton']/a[1]").click()
time.sleep(2)
drive.switch_to_alert().accept()