【基础】练习册62-Python3_web_上传文件&多窗口切换
上传文件
代码如下:
#!coding=utf-8
#上传文件---上传失败
from selenium import webdriver
import os
driver = webdriver.Chrome()
#打开编写好的上传功能页面
file_path = 'file:///'+os.path.abspath('upfile.html')
driver.get(file_path)
#定位上传按钮
driver.find_element_by_link_text('选择文件').send_keys('D:\text.txt')
driver.quit()
多窗口切换
代码如下:
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.implicitly_wait(10)
driver.get("http://www.baidu.com")
search_window = driver.current_window_handle #获得百度搜索窗口句柄
driver.find_element_by_link_text(u'登录').click()
driver.find_element_by_link_text(u'立即注册').click()
all_handles = driver.window_handles #获得当前所有打开窗口的句柄
for handle in all_handles: #进入注册窗口
if handle!=search_window:
driver.switch_to_window(handle)
print("now register window!")
driver.find_element_by_name('userName').send_keys('ccyixuan')
driver.find_element_by_name('password').send_keys('password')
for handle in all_handles:#进入搜索界面
if handle == search_window:
driver.switch_to_window(handle)
print("now search window")
driver.find_element_by_id('TANGRAM__PSP_2__closeBth').click()
driver.find_elements_by_id('kw').send_keys("selenium")
driver.find_elements_by_id('su').click()
time.sleep(5)
driver.quit()