python爬虫第十三讲 - selenium中

selenium

定位元素

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
# 定位输入框
# 通过id定位
# driver.find_element_by_id('kw').send_keys('原神')
# driver.find_element(By.ID, 'kw').send_keys('原神')

# 通过name定位
# driver.find_elements_by_name('wd')[0].send_keys('原神')
# driver.find_element_by_name('wd').send_keys('原神')
# driver.find_element(by=By.NAME, value='wd').send_keys('原神')

# 通过class定位
# driver.find_element_by_class_name('s_ipt').send_keys('原神')

# 通过xpath定位
# driver.find_element_by_xpath('//*[@id="kw"]').send_keys('原神')

# 通过css定位
driver.find_element_by_css_selector('#kw').send_keys('原神')

操作元素

  • send_keys() 向一些标签来输入内容(input)
# 定位百度一下按钮,输入内容
driver.find_element_by_id('su').send_keys('一下百度')  # input标签可以执行
  • clear()清空元素
inputTag = driver.find_element_by_id('kw')
inputTag.send_keys('原神')
time.sleep(1)
inputTag.clear()
  • click() 点击
inputTag = driver.find_element_by_id('kw')
inputTag.send_keys('原神')
time.sleep(1)
driver.find_element_by_id('su').click()

操作下拉菜单

素材 https://www.17sucai.com/boards/53562.html

  1. 操作select标签
driver = webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换iframe标签
driver.switch_to.frame(driver.find_element_by_id('iframe'))
selectTag = Select(driver.find_element_by_class_name('nojs'))
# 选择标签
# selectTag.select_by_value('JP')
selectTag.select_by_index(4)  # 或者通过索引选择
  1. 非select标签
driver = webdriver.Chrome()
driver.get('https://www.17sucai.com/pins/demo-show?id=5926')
# 切换iframe标签
driver.switch_to.frame(driver.find_element_by_id('iframe'))
time.sleep(1)
driver.find_element_by_class_name('dk_toggle').click()  # 注意标签里有空格只要选部分,不带空格的就行了
time.sleep(1)
driver.find_element_by_xpath('//*[@id="dk_container_country-nofake"]/div/ul/li[5]/a').click()

模拟登录豆瓣

from selenium import webdriver
import time

driver = webdriver.Chrome()
# 打开网站
driver.get('https://www.douban.com/')
# 切换iframe
driver.switch_to.frame(driver.find_element_by_xpath('//*[@id="anony-reg-new"]/div/div[1]/iframe'))
time.sleep(1)
# 点击密码登陆的字
driver.find_element_by_class_name('account-tab-account').click()
time.sleep(1)
# 输入账号密码
driver.find_element_by_id('username').send_keys('xxxxxx')
time.sleep(1)
driver.find_element_by_id('password').send_keys('123456')
time.sleep(1)

# 点击登陆按钮
driver.find_element_by_class_name('btn-account').click()

selenium获取cookie

如果要通过selenium获取cookie要做一些数据处理
1 如何获取cookie
driver.get_cookies()

2 获取到的cookie如何处理 基础内容
cookie = [item[‘name’] + ‘=’ + item[‘value’] for item in listCookies]
cookie_str = '; '.join(item for item in cookie)

from selenium import webdriver

driver = webdriver.Chrome()
driver.get('https://www.baidu.com')

cookies = driver.get_cookies()

cookie_lst = [item['name']+'='+item['value']for item in cookies]
cookie_str = '; '.join(cookie_lst)

# 存储cookie
with open('cookie.txt','w',encoding='utf-8') as f:
    f.write(cookie_str)
上一篇:node: 使用res.send()时报错RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code:18


下一篇:send|recv详解