python + selenium

# -*- coding: gbk -*-
#encoding=utf8
# -*- coding:cp936 -*-
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
import sys
reload(sys)
sys.setdefaultencoding('gbk')
driver =webdriver.Firefox()
driver.get("https://www.baidu.com")
print driver.title
ekey=driver.find_element_by_name("wd")
str1= '人'
str1=str1.encode('UTF-8')  
print str1.decode("UTF-8")
print str1
ekey.send_keys(str1.decode("UTF-8"))
time.sleep(1)
str1="工"
str1=str1.encode('UTF-8')  
ekey.send_keys(str1.decode("UTF-8"))
time.sleep(1)
str1="智能"
str1=str1.encode('UTF-8')  
ekey.send_keys(str1.decode("UTF-8"))
time.sleep(1)
ekey.send_keys(Keys.RETURN)
time.sleep(5)
str1="人工智能_百度百科"
str1=str1.encode('UTF-8')  
e1=driver.find_element("link text",str1.decode("utf-8"))
e1.click()
e1.click()
time.sleep(5)
print driver.current_window_handle # 输出当前窗口句柄(百度)
handles = driver.window_handles # 获取当前窗口句柄集合(列表类型)
print handles # 输出句柄集合
for handle in handles:# 切换窗口(切换到搜狗)
    if handle!=driver.current_window_handle:
        print 'switch to ',handle
        driver.switch_to_window(handle)
        print driver.current_window_handle # 输出当前窗口句柄(搜狗)
        break


driver.close() #关闭当前窗口
driver.switch_to_window(handles[0]) #切换回百度窗口
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
time.sleep(10)
driver.quit()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值