04-selenium等待元素加载、元素使用、xpath的使用

发布时间 2024-01-02 11:28:23作者: Way*yy

selenium等待元素加载

# 代码操作非常快---》有的标签还没加载---》找就找不到---》就会报错
# 设置等待:显示等待,隐士等待
bro.implicitly_wait(10) # 找某个标签,如果找不到,最多等待10s

selenium元素操作

# 点击操作
	click()
# 写文字
	send_keys("内容")
# 清空文字
	clear()

执行js

# 在使用selenium操作浏览器的时候,可以自己写js执行,会用这东西做什么?
    -创建新的选项卡
    -打印出一些变量(属于当前爬取的页面中的变量)
    -获取当前登录的cookie
    -滑动屏幕
import requests
import time
from selenium import webdriver
from selenium.webdriver.common.by import By

bro = webdriver.Chrome()
bro.get('https://www.pearvideo.com/category_1')
# 等待加载JS数据
bro.implicitly_wait(10)
# 最大化窗口
bro.maximize_window()
time.sleep(5)
# 在浏览器中执行JS代码
# bro.execute_script('alert("美女")')
# 打开一个新的标签页
bro.execute_script('open()')
# 滑动屏幕
# bro.execute_script('scrollTo(0,document.documentElement.scrollHeight)')

# 获取当前访问地址
# bro.execute_script('alert(location)')
# bro.execute_script('location="http://www.baidu.com"')
# 打印cookie
bro.execute_script('alert(document.cookie)')
time.sleep(10)
bro.close()

切换选项卡

from selenium import webdriver
import time

bro = webdriver.Chrome()
bro.implicitly_wait(10)
bro.maximize_window()
bro.execute_script("open()")
time.sleep(1)
bro.switch_to.window(bro.window_handles[1])  # 切换选项卡
bro.get('https://www.taobao.com')

time.sleep(2)
bro.switch_to.window(bro.window_handles[0])  # 切换到某个选项卡
bro.get('https://www.baidu.com')
time.sleep(2)
bro.execute_script('window.open()')
bro.execute_script('window.open()')
bro.close()  # 关闭选项卡
bro.quit()  # 关闭页面

登录cnblogs

import json
import time

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(options=options)

bro.get("https://www.cnblogs.com/")
bro.maximize_window()
bro.implicitly_wait(10)

phone_input = bro.find_element(By.LINK_TEXT, '登录')
phone_input.click()

sms_login = bro.find_element(By.CSS_SELECTOR, "#mat-tab-label-0-1 > div")
sms_login.click()

username_input = bro.find_element(By.ID, "mat-input-3")
submit = bro.find_element(By.CSS_SELECTOR,
                          "#mat-tab-content-0-1 > div > div > app-verification-code-input > mat-form-field > div > "
                          "div.mat-form-field-flex.ng-tns-c47-8 > "
                          "div.mat-form-field-suffix.ng-tns-c47-8.ng-star-inserted > button")
code = bro.find_element(By.CSS_SELECTOR, "#mat-input-4")

username_input.send_keys("18436093205")
submit.click()
codes = bro.find_element(By.ID, "rectMask")
codes.click()
try:
    code_sms = input("请输入验证码:")
    code.send_keys(code_sms)
except Exception:
    print("验证码错误")

login = bro.find_element(By.CSS_SELECTOR,
                         "body > app-root > app-sign-in-layout > div > div > app-sign-in > app-content-container > "
                         "div > div > div > form > div > button")
login.click()
time.sleep(10)
cookies = bro.get_cookies()
with open("cnblogs.json", "w", encoding="utf8") as f:
    json.dump(cookies, f)
time.sleep(2)
bro.close()

抽屉半自动点赞

# 使用selenium登录--->拿到cookie
# 点赞 使用requests 用cookie点赞
####自动登录---使用selenium####
import json

# import time
#
# from selenium import webdriver
# from selenium.webdriver.chrome.options import Options
# import json
# from selenium.webdriver.common.by import By
#
# bro = webdriver.Chrome()
# bro.get('https://dig.chouti.com/')
# bro.implicitly_wait(10)
# bro.maximize_window()
#
# btn_login = bro.find_element(By.ID, 'login_btn')
# time.sleep(1)
# btn_login.click()
# time.sleep(1)
#
# phone = bro.find_element(By.NAME, 'phone')
# password = bro.find_element(By.CSS_SELECTOR,
#                             'body > div.login-dialog.dialog.animated2.scaleIn > div > div.login-footer > div.form-item.login-item.clearfix.mt24 > div > input.input.pwd-input.pwd-input-active.pwd-password-input')
#
# submit_login = bro.find_element(By.CSS_SELECTOR,
#                                 'body > div.login-dialog.dialog.animated2.scaleIn > div > div.login-footer > div:nth-child(4) > button')
#
# phone.send_keys('18953675221')
# password.send_keys('lqz123')
# time.sleep(2)
# submit_login.click()
#
# input('等你')
#
# cookies = bro.get_cookies()
# with open('chouti.json', 'w', encoding='utf-8') as f:
#     json.dump(cookies, f)
#
# time.sleep(2)
# bro.close()


#### 使用requests点赞

# 访问首页,解析出id号
import requests
from bs4 import BeautifulSoup

#### 携带cookie访问#####
session = requests.Session()
cookie = {}  # 本地取出来,写入
with open('chouti.json', 'r') as f:
    cookie_list = json.load(f)
##### selenium的cookie和requests的cookie格式不一样,要转换   {key:value,key:value}
for item in cookie_list:
    cookie[item['name']] = item['value']
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'}
res = session.get('https://dig.chouti.com/', cookies=cookie,headers=header)
soup = BeautifulSoup(res.text, 'html.parser')


print(res.text)


divs = soup.find_all(name='div', class_='link-item')
for div in divs:
    article_id = div.attrs.get('data-id')
    data = {
        'linkId': article_id
    }

    res1 = session.post('https://dig.chouti.com/link/vote', data=data,headers=header)
    print(res1.text)

xpath

# 在 xml中查找元素,解析库
	-解析库自带的
    -css选择器
    -xpath--->通用的--->
    	即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言
        
        
# 记住的:
	nodename	选取此节点的所有子节点。
    /	        从根节点选取。
    //	        从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置。
    .	        选取当前节点。
    ..	        选取当前节点的父节点。
    @	        选取属性。
    
    
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)
# html=etree.parse('search.html',etree.HTMLParser())
# 1 所有节点
# a=html.xpath('//*')
# 2 指定节点(结果为列表)
# a=html.xpath('//head')
# 3 子节点,子孙节点
# a=html.xpath('//div/a')
# a=html.xpath('//body/a') #无数据
# a=html.xpath('//body//a')
# 4 父节点
# a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath('//body//a[1]/..')
# 也可以这样
# a=html.xpath('//body//a[1]/parent::*')
# a=html.xpath('//body//a[1]/parent::div')
# 5 属性匹配
# a=html.xpath('//body//a[@href="image1.html"]')

# 6 文本获取     /text()
# a=html.xpath('//body//a[@href="image1.html"]/text()')

# 7 属性获取     @属性名
# a=html.xpath('//body//a/@href')
# # 注意从1 开始取(不是从0)
# a=html.xpath('//body//a[1]/@href')

# 8 属性多值匹配
#  a 标签有多个class类,直接匹配就不可以了,需要用contains
# a=html.xpath('//body//a[@class="li"]')
# a=html.xpath('//body//a[contains(@class,"li")]')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 9 多属性匹配
# a=html.xpath('//body//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 10 按序选择
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# 取最后一个
# a=html.xpath('//a[last()]/@href')
# a=html.xpath('//a[last()-1]/@href') # 倒数第二个
# 位置小于3的
# a = html.xpath('//a[position()<3]/@href')

# 倒数第三个
# a=html.xpath('//a[last()-2]/@href')
# 11 节点轴选择
# ancestor:祖先节点
# 使用了* 获取所有祖先节点
# a=html.xpath('//a/ancestor::*')
# # 获取祖先节点中的div
# a=html.xpath('//a/ancestor::div')
# attribute:属性值
# a=html.xpath('//a[1]/attribute::*')
# a=html.xpath('//a[1]/attribute::href')

# child:直接子节点
# a=html.xpath('//a[1]/child::*')
# descendant:所有子孙节点
# a=html.xpath('//a[6]/descendant::*')
# following:当前节点之后所有节点
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# following-sibling:当前节点之后同级节点
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]')
# a=html.xpath('//a[1]/following-sibling::*[2]/@href')

# print(a)


'''
/
//
.
..
取文本  /text()
取属性  /@属性名
根据属性过滤  [@属性名=属性值]
class 特殊
[contains(@class,"li")]
'''

动作链

# 模拟鼠标点住,拖动的效果,实现滑块认证

# 两种形式
	-形式一:
        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')  ##切换到iframeResult
    sourse = driver.find_element(By.ID, 'draggable')
    target = driver.find_element(By.ID, 'droppable')

    # 方式一:基于同一个动作链串行执行
    # actions = ActionChains(driver)  # 拿到动作链对象
    # actions.drag_and_drop(sourse, target)  # 把动作放到动作链中,准备串行执行
    # actions.perform()

    # 方式二:不同的动作链,每次移动的位移都不同
    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()

自动登录12306