本文主要是介绍python 定位元素,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
获取元素列表
gg = driver.find_elements_by_css_selector("div.offer-attr-item")
循环元素列表
for g in range(0,len(gg)):获取元素列表下面的元素,==定位元素后,可以继续定位gname = driver.find_elements_by_css_selector("div.offer-attr-item")[g].find_element_by_class_name("offer-attr-item-name").textgvalue = driver.find_elements_by_css_selector("div.offer-attr-item")[g].find_element_by_class_name("offer-attr-item-value").textprint(gname + '-'+ gvalue)下拉网页
js = "var q=document.documentElement.scrollTop=500"
driver.execute_script(js)正则
import re
num = re.findall(r"[0-9]{1,}\.[0-9]{1,}",'78.50元')
num1 = re.search(r"[0-9]{1,}[0-9]{1,}]",'aaaaaaaaa123.789dddddddddd')随机数
import random
print(random.random())
print(random.randint(10000,99999))整型和字符串拼接itime = datetime.datetime.now()
name = "拼多多订单"+ str(itime)随机选择列表
import random
list1 = ['佛山', '南宁', '北海', '杭州', '南昌', '厦门', '温州']
a = random.choice(list1)
print(a)返回json数据格式from django.http import JsonResponselist1 = ['佛山', '南宁', '北海', '杭州', '南昌', '厦门', '温州']
finalData = {'data': data }
return JsonResponse(finalData, safe=False) 获取网页tokentoken = driver.execute_script('return localStorage.getItem("AccessToken");')发送请求 带数据 header
import requests
url1 ="https://"headers = {'accesstoken':token
}data ={"offset": '',} #post参数r = requests.post(url1, headers=headers, data= data)
json_str= r.json() #提取json数据保存 excel
import xlwtworkbook = xlwt.Workbook(encoding='utf-8')
# 创建一个worksheet
worksheet = workbook.add_sheet('My Worksheet')
worksheet.write(0, 0, label='订单号')
worksheet.write(0, 1, label='运单号')
worksheet.write(0, 2, label='快递公司')
worksheet.write(0, 3, label='更新日期')
worksheet.write(0, 4, label='物流信息')
workbook.save('D:\\pdd\\' + name + '.xls')selenium 调用已经打开的网页
cmd打开一个端口为9527的浏览器
.\chrome.exe --remote-debugging-port=9527 --user-data-dir=“d:\selenium\AutomationProfile
url =""
options = Options()
options.add_experimental_option("debuggerAddress", "127.0.0.1:9527")
driver = webdriver.Chrome(options=options)
driver.get(url)使用cookie
from requests.cookies import RequestsCookieJarfr = open('taobbao.txt','r')
coojies = json.load(fr)
fr.close()
for cookie in coojies:driver.add_cookie(cookie)driver.refresh()保存cookie
dricookie = driver.get_cookies()
fw = open('taobbao.txt','w')
json.dump(dricookie,fw)
fw.close()
这篇关于python 定位元素的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!