当前位置:网站首页>selenium使用———xpath和模拟输入和模拟点击协作
selenium使用———xpath和模拟输入和模拟点击协作
2022-07-25 11:11:00 【时闻折竹】
目的
本文主要目的是使用xpath、模拟输入和模拟点击协作,具体如下:
- 打开百度,搜索CSDN
- 进入CSDN,搜索selenium使用———xpath和模拟输入和模拟点击协作这篇文章。
- 点击进入该文章进行查看
搜索CSDN
打开百度
browser.get("https://www.baidu.com/")
在输入框中输入CSDN
找到输入框。首先右键输入框,进入检查模式;其次右键对应的HTML代码,选择Copy->Copy XPath,最后得到XPath。
有了XPath后,可以使用Selenium进行定位,并把需要输入的内容填写上。
browser.find_element_by_xpath('//*[@id="kw"]').send_keys("CSDN")
点击搜索按钮
使用上述步骤找到按钮的XPath的值,并模拟点击
browser.find_element_by_xpath('//*[@id="su"]').click()
进入CSDN
判断页面是否加载完成
选择页面中你要点击的元素的id,并以此判断该网页是否完成加载。这里我使用了页面中id为content_left的元素用于判断我需要点击的组件是否加载完成
print("开始加载")
Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "content_left")))
print("加载结束")
在得到需要点击的组件加载完成的信息后,使用上述步骤找到CSDN网页的XPath的值,并点击进入
browser.find_element_by_xpath('//*[@id="1"]/div/div[1]/h3/a[1]').click()
从CSDN首页进行文章搜索
判断需要使用的部分是否加载完成
print("开始加载")
Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "toolbar-search-button")))
print("加载结束")
输入信息并点击搜索
browser.find_element_by_xpath('//*[@id="toolbar-search-input"]').send_keys("selenium使用———xpath和模拟输入和模拟点击协作")
browser.find_element_by_xpath('//*[@id="toolbar-search-button"]').click()
进入文章并滚动
print("开始加载")
Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "post-1562")))
print("加载结束")
# # 直接拉到底
# js = 'window.scrollTo(0, document.body.scrollHeight)'
# browser.execute_script(js)
# 缓慢拉动
js = "return action=document.body.scrollHeight"
new_height = browser.execute_script(js)
for i in range(0,new_height,10):
browser.execute_script('window.scrollTo(0, %s)'%(i))
全部代码
有点乱
#coding=utf-8
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as Wait
import time
# 初始化浏览器
browser = webdriver.Chrome()
# # 使用百度搜索CSDN
# browser.get("https://www.baidu.com/")
# # 找到输入框并输入
# browser.find_element_by_xpath('//*[@id="kw"]').send_keys("CSDN")
# # 找到按钮并点击
# browser.find_element_by_xpath('//*[@id="su"]').click()
# # 进入CSDN
# print("开始加载")
# Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "content_left")))
# print("加载结束")
# browser.find_element_by_xpath('//*[@id="1"]/div/div[1]/h3/a[1]').click()
# # 在CSDN中搜索指定文章
# print("开始加载")
# Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "toolbar-search-button")))
# print("加载结束")
# browser.find_element_by_xpath('//*[@id="toolbar-search-input"]').send_keys("selenium使用———xpath和模拟输入和模拟点击协作")
# browser.find_element_by_xpath('//*[@id="toolbar-search-button"]').click()
# browser.get('https://so.csdn.net/so/search?spm=1000.2115.3001.4501&q=aa&t=&u=')
# print("开始加载")
# Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "app")))
# print("加载结束")
# browser.find_element_by_xpath('//*[@id="app"]/div[2]/div[2]/div[1]/div[2]/div/div[1]/div/div[1]/h3').click()
browser.get('http://www.selenium.org.cn/1562.html')
print("开始加载")
Wait(browser, 60).until(EC.presence_of_element_located((By.ID, "post-1562")))
print("加载结束")
# # 直接拉到底
# js = 'window.scrollTo(0, document.body.scrollHeight)'
# browser.execute_script(js)
# 缓慢拉动
js = "return action=document.body.scrollHeight"
new_height = browser.execute_script(js)
for i in range(0,new_height,10):
browser.execute_script('window.scrollTo(0, %s)'%(i))
# browser.quit()
边栏推荐
- Management of software defects
- Varest blueprint settings JSON
- [electronic device notes 5] diode parameters and selection
- 【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
- 【高并发】我用10张图总结出了这份并发编程最佳学习路线!!(建议收藏)
- JS scope and pre parsing
- I advise those students who have just joined the work: if you want to enter the big factory, you must master these concurrent programming knowledge! Complete learning route!! (recommended Collection)
- Innovation and breakthrough! AsiaInfo technology helped a province of China Mobile complete the independent and controllable transformation of its core accounting database
- Oil monkey script link
- flink sql client 连接mysql报错异常,如何解决?
猜你喜欢

【AI4Code】《Unified Pre-training for Program Understanding and Generation》 NAACL 2021

Onenet platform control w5500 development board LED light

30 sets of Chinese style ppt/ creative ppt templates
![[comparative learning] understanding the behavior of contractual loss (CVPR '21)](/img/96/9b58936365af0ca61aa7a8e97089fe.png)
[comparative learning] understanding the behavior of contractual loss (CVPR '21)

【GCN多模态RS】《Pre-training Representations of Multi-modal Multi-query E-commerce Search》 KDD 2022

Intelligent information retrieval(智能信息检索综述)

创新突破!亚信科技助力中国移动某省完成核心账务数据库自主可控改造

brpc源码解析(二)—— brpc收到请求的处理过程

brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制

【CTR】《Towards Universal Sequence Representation Learning for Recommender Systems》 (KDD‘22)
随机推荐
JS 面试题:手写节流(throttle)函数
PL/SQL入门,非常详细的笔记
Power BI----这几个技能让报表更具“逼格“
【6篇文章串讲ScalableGNN】围绕WWW 2022 best paper《PaSca》
【GCN-RS】Towards Representation Alignment and Uniformity in Collaborative Filtering (KDD‘22)
The JSP specification requires that an attribute name is preceded by whitespace
JS常用内置对象 数据类型的分类 传参 堆栈
Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot
Arrays in JS
知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)
php 一台服务器传图片到另一台上 curl post file_get_contents保存图片
【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)
【高并发】SimpleDateFormat类到底为啥不是线程安全的?(附六种解决方案,建议收藏)
Intelligent information retrieval(智能信息检索综述)
dirReader. Readentries compatibility issues. Exception error domexception
程序员送给女孩子的精美礼物,H5立方体,唯美,精致,高清
【AI4Code】CodeX:《Evaluating Large Language Models Trained on Code》(OpenAI)
PHP one server sends pictures to another. Curl post file_ get_ Contents save pictures
brpc源码解析(八)—— 基础类EventDispatcher详解
Objects in JS