当前位置:网站首页>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()
边栏推荐
- The bank's wealth management subsidiary accumulates power to distribute a shares; The rectification of cash management financial products was accelerated
- Brpc source code analysis (V) -- detailed explanation of basic resource pool
- 知识图谱用于推荐系统问题(MVIN,KERL,CKAN,KRED,GAEAT)
- JS operator
- 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)
- JS data types and mutual conversion
- 【图攻防】《Backdoor Attacks to Graph Neural Networks 》(SACMAT‘21)
- [cloud co creation] what is the role of AI in mathematics? What will be the disruptive impact on the mathematical world in the future?
- 【Debias】Model-Agnostic Counterfactual Reasoning for Eliminating Popularity Bias in RS(KDD‘21)
- 【AI4Code】《CoSQA: 20,000+ Web Queries for Code Search and Question Answering》 ACL 2021
猜你喜欢

【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)

【GCN-RS】Are Graph Augmentations Necessary? Simple Graph Contrastive Learning for RS (SIGIR‘22)

JS data types and mutual conversion

【GCN】《Adaptive Propagation Graph Convolutional Network》(TNNLS 2020)

brpc源码解析(六)—— 基础类socket详解

"Mqtt protocol explanation and Practice (access to onenet)" of wiznet w5500 series training activities

【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22

JS operator

LeetCode第303场周赛(20220724)

brpc源码解析(一)—— rpc服务添加以及服务器启动主要过程
随机推荐
【GCN-RS】Learning Explicit User Interest Boundary for Recommendation (WWW‘22)
银行理财子公司蓄力布局A股;现金管理类理财产品整改加速
[electronic device notes 5] diode parameters and selection
Transformer变体(Routing Transformer,Linformer,Big Bird)
【GCN多模态RS】《Pre-training Representations of Multi-modal Multi-query E-commerce Search》 KDD 2022
Attendance system based on w5500
winddows 计划任务执行bat 执行PHP文件 失败的解决办法
30套中国风PPT/创意PPT模板
Web APIs(获取元素 事件基础 操作元素)
【AI4Code】《Contrastive Code Representation Learning》 (EMNLP 2021)
【GCN-CTR】DC-GNN: Decoupled GNN for Improving and Accelerating Large-Scale E-commerce Retrieval WWW22
Brpc source code analysis (VII) -- worker bthread scheduling based on parkinglot
LeetCode第303场周赛(20220724)
【GCN-RS】MCL: Mixed-Centric Loss for Collaborative Filtering (WWW‘22)
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
[MySQL 17] installation exception: could not open file '/var/log/mysql/mysqld log‘ for error logging: Permission denied
brpc源码解析(三)—— 请求其他服务器以及往socket写数据的机制
Solved files' name is invalid or doors not exist (1205)
Brpc source code analysis (VI) -- detailed explanation of basic socket
JS operator