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"])