#!/usr/bin/env python
# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

import time,os

# about:addons 火狐浏览器安装组件,访问的地址

# <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off">
# <input type="submit" value="百度一下" id="su" class="btn self-btn bg s_btn">
#id
keys="工作"
delay=3
url="http://sz.ganji.com/"
driver=webdriver.Firefox()

driver.get(url)

#等待delay秒
driver.implicitly_wait(delay)

#当前窗口句柄
cur_handle=driver.current_window_handle

#获取所有句柄
driver.find_element_by_link_text(keys).click()
all_handle=driver.window_handles

# #切换句柄方法1
for i in all_handle:
if i!=cur_handle:
driver.switch_to.window(i)
#
# #切换句柄方法2
driver.switch_to.window(all_handle[1])
print(driver.title)

# 切回到首页窗口
driver.close()
driver.switch_to.window(cur_handle)
print(driver.title)

#等待delay秒
time.sleep(delay)

# # 退出当前窗口
driver.close()

# 退出所有窗口
driver.quit()


posted on 2018-12-04 12:12  新美好时代  阅读(153)  评论(0编辑  收藏  举报