1 selenium等待元素加载
browser.implicitly_wait(10 )
2 selenium元素操作
tag.send_keys(写文字)
tag.clear()
tag.click
3 selenium执行js
from selenium import webdriver
import time
bro = webdriver.Chrome()
bro.get('https://www.pearvideo.com/' )
bro.implicitly_wait(10 )
bro.execute_script('scrollTo(0,document.documentElement.scrollHeight)' )
time.sleep(5 )
bro.close()
bro.quit()
4 selenium切换选项卡
from selenium import webdriver
import time
bro = webdriver.Chrome()
bro.get('https://www.pearvideo.com/' )
bro.implicitly_wait(10 )
print (bro.window_handles)
bro.execute_script('window.open()' )
bro.switch_to.window(bro.window_handles[1 ])
bro.get('http://www.taobao.com' )
time.sleep(2 )
bro.switch_to.window(bro.window_handles[0 ])
bro.get('http://www.baidu.com' )
time.sleep(2 )
bro.close()
bro.quit()
5 selenium前进后退,异常处理
bro.back()
time.sleep(2 )
bro.forward()
6 selenium登录cnblogs
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import json
bro = webdriver.Chrome()
bro.get('https://www.cnblogs.com/' )
bro.implicitly_wait(5 )
bro.maximize_window()
time.sleep(3 )
with open ('./cnblogs.json' ,'r' ,encoding='utf-8' ) as f:
cookies=json.load(f)
for item in cookies:
bro.add_cookie(item)
bro.refresh()
time.sleep(5 )
bro.close()
7 抽屉半自动点赞
import time
import json
import requests
from selenium import webdriver
from selenium.webdriver.common.by import By
header = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' ,
}
with open ('./chouti.json' , 'r' , encoding='utf-8' ) as f:
cookies = json.load(f)
real_cookie = {}
for item in cookies:
real_cookie[item['name' ]] = item['value' ]
print (real_cookie)
res = requests.get('https://dig.chouti.com/top/24hr?_=1689043464339' , headers=header).json()
for item in res.get('data' ):
link_id = item.get('id' )
data = {
'linkId' : link_id
}
res = requests.post('https://dig.chouti.com/link/vote' , headers=header, data=data, cookies=real_cookie)
print (res.text)
8 xpath使用
-css选择器
-xpath:XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言
div 选取div标签
/ 从根节点选取
// 从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
. 选取当前节点。
.. 选取当前节点的父节点。
@ 选取属性
doc = '''
<html>
<head>
<base href='http://example.com/' />
<title>Example website</title>
</head>
<body>
<div id='images'>
<a href='image1.html' id='lqz'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
<a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
<a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
<a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
<a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
<a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
</div>
</body>
</html>
'''
from lxml import etree
html = etree.HTML(doc)
'''
/
//
.
..
取文本 /text()
取属性 /@属性名
根据属性过滤 [@属性名=属性值]
class 特殊
[contains(@class,"li")]
'''
9 动作链
-形式一:
actions=ActionChains(bro)
actions.drag_and_drop(sourse,target)
actions.perform()
-方式二:
ActionChains(bro).click_and_hold(sourse).perform()
distance=target.location['x' ]-sourse.location['x' ]
track=0
while track < distance:
ActionChains(bro).move_by_offset(xoffset=2 ,yoffset=0 ).perform()
track+=2
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
import time
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable' )
driver.implicitly_wait(3 )
driver.maximize_window()
try :
driver.switch_to.frame('iframeResult' )
sourse = driver.find_element(By.ID, 'draggable' )
target = driver.find_element(By.ID, 'droppable' )
ActionChains(driver).click_and_hold(sourse).perform()
distance=target.location['x' ]-sourse.location['x' ]
track = 0
while track < distance:
ActionChains(driver).move_by_offset(xoffset=2 , yoffset=0 ).perform()
track += 2
ActionChains(driver).release().perform()
time.sleep(10 )
finally :
driver.close()
10 自动登录12306
import time
from selenium.webdriver import ActionChains
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--disable-blink-features=AutomationControlled" )
bro = webdriver.Chrome(chrome_options=options)
bro.get('https://kyfw.12306.cn/otn/resources/login.html' )
bro.implicitly_wait(5 )
bro.maximize_window()
user_login = bro.find_element(By.CSS_SELECTOR,
'#toolbar_Div > div.login-panel > div.login-box > ul > li.login-hd-code.active > a' )
user_login.click()
time.sleep(1 )
username = bro.find_element(By.ID, 'J-userName' )
password = bro.find_element(By.ID, 'J-password' )
submit_btn = bro.find_element(By.ID, 'J-login' )
username.send_keys('18953675221' )
password.send_keys('' )
time.sleep(3 )
submit_btn.click()
time.sleep(5 )
span = bro.find_element(By.ID, 'nc_1_n1z' )
ActionChains(bro).click_and_hold(span).perform()
ActionChains(bro).move_by_offset(xoffset=300 , yoffset=0 ).perform()
ActionChains(bro).release().perform()
time.sleep(5 )
bro.close()
11 打码平台
-简单的数字字母
-高级一些的,计算题,成语
-选中图中的 公交车
。。。
12 打码平台自动登录
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from chaojiying import ChaojiyingClient
from PIL import Image
bro = webdriver.Chrome()
bro.get('http://www.chaojiying.com/apiuser/login/' )
bro.implicitly_wait(10 )
bro.maximize_window()
try :
username = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[1]/input' )
password = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[2]/input' )
code = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[3]/input' )
btn = bro.find_element(by=By.XPATH, value='/html/body/div[3]/div/div[3]/div[1]/form/p[4]/input' )
username.send_keys('306334678' )
password.send_keys('lqz123' )
bro.save_screenshot('main.png' )
img = bro.find_element(By.XPATH, '/html/body/div[3]/div/div[3]/div[1]/form/div/img' )
location = img.location
size = img.size
print (location)
print (size)
img_tu = (int (location['x' ]), int (location['y' ]), int (location['x' ] + size['width' ]), int (location['y' ] + size['height' ]))
img = Image.open ('./main.png' )
fram = img.crop(img_tu)
fram.save('code.png' )
chaojiying = ChaojiyingClient('3053345678' , 'lqz123' , '950575' )
im = open ('code.png' , 'rb' ).read()
print (chaojiying.PostPic(im, 1902 ))
res_code=chaojiying.PostPic(im, 1902 )['pic_str' ]
code.send_keys(res_code)
time.sleep(5 )
btn.click()
time.sleep(10 )
except Exception as e:
print (e)
finally :
bro.close()
本文作者:岳宗柯
本文链接:https://www.cnblogs.com/yuezongke/p/17545792.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议 进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步