python3 xpath selenium BeautifulSoup 常用方法

XPath

注意s
find_elements_by_xpath  
find_element_by_xpath
find_element
find_elements

 elements=driver.find_elements_by_xpath('//ul[@id="haltList"]/li')
    stop_list_stats=[]
    for i in elements:        
        i.find_element(By.TAG_NAME, "a").click()
        

XPath轴(XPath Axes)可定义某个相对于当前节点的节点集: 
1、child 选取当前节点的所有子元素 
2、parent 选取当前节点的父节点 
3、descendant 选取当前节点的所有后代元素(子、孙等) 
4、ancestor 选取当前节点的所有先辈(父、祖父等) 
5、descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身 
6、ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身 
7、preceding-sibling 选取当前节点之前的所有同级节点 
8、following-sibling 选取当前节点之后的所有同级节点 
9、preceding 选取文档中当前节点的开始标签之前的所有节点 
10、following 选取文档中当前节点的结束标签之后的所有节点 
11、self 选取当前节点 
12、attribute 选取当前节点的所有属性 
13、namespace 选取当前节点的所有命名空间节点 
如:要定位当前td同级后的一个td

//td[.='text']/following-sibling::td

selenium 原生用法

 

BeautifulSoup 用法

可以使用自动化 Selenium IDE 导出代码:

#!/usr/bin/env/python
# -*-coding:utf-8-*-

# 使用的自动化录制  Selenium IDE
# pytest 扩展的安装
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from time import sleep

class TestTest():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_test(self):
    # Test name: test
    # Step # | name | target | value
     略......

if __name__=="__main__":
    pytest.main(["-s","autopytest.py"])

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值