1.selenium相关操作
from selenium import webdriver import time brower = webdriver.Chrome(executable_path='./chromedriver.exe') # 引入chrome驱动 brower .maximize_window() # 窗口最大化 brower .get('https://www.taobao.com/') # get方式请求淘宝 time.sleep(3) search_input = brower .find_element_by_id('q') # 根据id找到淘宝搜索框 search_input.send_keys("iphone 手机壳") # 在框中输入搜索内容 brower .execute_script('window.scrollTo(0,document.body.scrollHeight)') # 执行js代码,滚动屏幕 btn = brower .find_element_by_css_selector('.btn-search') # 根据标签找到按钮 btn.click() # 点击 time.sleep(2) brower .get("https://www.sogou.com/") time.sleep(2) brower .back() # 页面后退 time.sleep(2) brower .forward() # 页面前进 time.sleep(5) brower .quit() # 退出浏览器
2.selenium中的iframe操作
如果定位的标签在iframe中,需要用bro.switch_to.frame(id)切换到指定id的iframe作用域中
from selenium import webdriver from selenium.webdriver import ActionChains # 引包 import time bro = webdriver.Chrome(executable_path='./chromedriver.exe') bro.maximize_window() bro.get('https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') bro.switch_to.frame('iframeResult') # 切换浏览器标签的作用域 div = bro.find_element_by_id('draggable') action = ActionChains(bro) # 实例化 action.click_and_hold(div) # 点击并保持 for i in range(5): # perform()立即执行动作链操作 # move_by_offset(x,y); x 水平方向,y 竖直方向 action.move_by_offset(14, 0).perform() time.sleep(0.3) # 释放动作链 action.release() bro.quit()