selenium+python百度注册页面切换+alter处理

注册和搜索页面切换

'''
百度的注册和搜索页面切换
'''
# -*- coding: utf-8 -*-
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() # 通过id定位

警告弹出处理

'''
百度的设置警告处理
'''
# -*- coding: utf-8 -*-
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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值