说明
使用chrome浏览器。
环境
参考 selenium+python爬虫全流程教程 前面 的安装过程
思路
查看浏览器chrome 版本,这里是 98.0.4758.102, 去官网下载 驱动程序
安装python 依赖包 selenium,这里装完后是 4.3.0
使用
工程路径 D:/1_common/python/code/projects/Bigdata2022/Crawer
# -*- coding: utf-8 -*- from selenium import webdriver import time from selenium.webdriver import Keys from selenium.webdriver.common.by import By driver = webdriver.Chrome(r"E:/Programs/chromedriver_win32/chromedriver.exe") driver.get("http://www.baidu.com") driver.implicitly_wait(10) # search_text = driver.find_element(By.ID, 'kw') # 输入框输入内容 search_text.send_keys("seleniumm") # 删除多输入的一个 m search_text.send_keys(Keys.BACK_SPACE) search_text.submit() time.sleep(7) driver.quit()
View Code
# -*- coding: utf-8 -*- from selenium import webdriver import time from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome(r"E:/Programs/chromedriver_win32/chromedriver.exe") driver.get("http://www.baidu.com") # 定位到要悬停的元素, 这里根据超链接的文本内容定位元素 above = driver.find_element(By.LINK_TEXT, '地图') # move_to_element 这里有很多其他 动作, 鼠标、键盘都有 # 对定位到的元素执行鼠标悬停操作 ActionChains(driver).move_to_element(above).perform() time.sleep(3) above = driver.find_element(By.LINK_TEXT, '视频') ActionChains(driver).move_to_element(above).perform() time.sleep(10) driver.quit()
View Code
# -*- coding: utf-8 -*- from selenium import webdriver import time from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver import Keys from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service # driver = webdriver.Chrome(r"E:/Programs/chromedriver_win32/chromedriver.exe") s = Service(r"E:/Programs/chromedriver_win32/chromedriver.exe") driver = webdriver.Chrome(service=s) driver.get("http://www.baidu.com") # 每隔开0.5秒检查 指定元素是否出现,最多等5秒 element = WebDriverWait(driver, 5, 0.5).until( EC.presence_of_element_located((By.ID, "kw")) ) element.send_keys('selenium') element.submit() time.sleep(7) driver.quit()
View Code
资料
Selenium Python 教程, 九四干, 2022-05-05
selenium的八种定位方式之:id、name、tag_name、class_name、link_text、partial_link_text、xpath,进击的小陈,2021-10-06
<iframe class=”trans-ext__iframe” style=”pointer-events: auto”></iframe>
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/276307.html