自动化测试selenium + request + 动态加载页面
# demo01
from selenium import webdriver
from time import sleep
bro = webdriver.Chrome(executable_path='/Users/ls/Desktop/爬虫+新工具/chromedriver')
bro.get(url='https://www.baidu.com/')
#找到查找标签
text_input=bro.find_element_by_id('kw')
#添加搜索内容
text_input.send_keys('人民币')
sleep(1)
#点击提交按钮
bro.find_element_by_id('su').click()
# 取出文本资源
data=bro.page_source
# 截图
bro.save_screenshot('1.png')
# 切换到下一个页面
# 打印页面列表
print(bro.window_handles)
# 跳转
bro.switch_to.window(bro.window_handles[1])
需要下载webdriver 对应自己的谷歌浏览器版本下载 http://chromedriver.storage.googleapis.com/index.html
url = 'https://movie.douban.com/typerank?type_name=%E6%83%8A%E6%82%9A&type=19&interval_id=100:90&action='
bro = webdriver.Chrome(executable_path=r'C:UsersAdministratorDesktop爬虫+数据day_03_爬虫chromedriver.exe')
bro.get(url)
sleep(3)
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(3)
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(3)
bro.execute_script('window.scrollTo(0,document.body.scrollHeight)')
sleep(2)
page_text = bro.page_source
with open('./douban.html','w',encoding='utf-8') as fp:
fp.write(page_text)
sleep(1)
bro.quit()
#隐藏浏览器 from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu')
#定位到一个具体的iframe bro.switch_to.frame('login_frame')