selenium使用

selenium使用

导入

from selenium import webdriver

使用

drive_path=r"C:\Program Files\Python37\Chrome Drive\chromedriver.exe"  # 驱动路径
driver=webdriver.Chrome(drive_path)  # 创建实例对象

打开网页

driver.get("http://www.baidu.com")  #打开页面

获取元素

inputTag=driver.find_element_by_id("kw")  # 通过id获取元素
inputTag=driver.find_element_by_name("wd")  # 通过name获取元素
inputTag=driver.find_element_by_class_name("s_ipt")  # 通过class_name获取元素
inputTag=driver.find_element_by_css_selector(".s_ipt")  # 通过class_name获取元素
inputTag=driver.find_element_by_link_text("新闻")   # 通过link_text连接文本获取元素
inputTag=driver.find_element_by_partial_link_text("闻")  # partial_link选中连接文本中部分定位
inputTag=driver.find_element_by_xpath('//*[@id="kw"]')  #通过xpath定位

对获取的元素传入值

inputTag.send_keys("python")

关闭页面

driver.close()  # 关闭当前页面
driver.quit()  #退出浏览器

清除元素

inputTag.clear()  #清除输入框内容

完整的步骤

from selenium import webdriver
from selenium.webdriver.support.ui import Select  # 导入表单选择

import time
#创建实例
driver_path=r"C:\Program Files\Python37\Chrome Drive\chromedriver.exe"  #驱动路径
chrome_drive=webdriver.Chrome(executable_path=driver_path)
chrome_drive.get("http://www.baidu.com")
#设置窗口大小
chrome_drive.maximize_window()  # 窗口最大化
chrome_drive.
#定位元素
inputTag=chrome_drive.find_element_by_id("kw")   # 获取元素
inputTag.send_keys("python3")   # 对输入框发送信息

# 获取cookie
cookies_list=chrome_drive.get_cookies()
print("cookies_list: {}".format(cookies_list))

cookies_dict={cookie["name"]:cookie["value"] for cookie in cookies_list}
print("cookies_dict: {}".format(cookies_dict))

time.sleep(2)
page_source=chrome_drive.page_source  #获取网页源码 获得与elements内容一致
with open("./page_source01.html","w",encoding="utf8") as f:
    f.write(page_source)


page_current_url=chromedrive.current_url  #获取当前网页连接
with open("./page_current_url.html","w",encoding="utf8") as f:
    f.write(page_current_url)
chromedrive.save_screenshot("baidu.png")  # 截屏
chromedrive.quit()

SSL验证警告和重定向

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)  # 取消SSL警告
verify=False  # 关闭ssl验证
allow_redirects=False  # 禁止重定向

使用无头浏览器

#使用谷歌无头浏览器
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

driver = webdriver.Chrome(r'chromedriver.exe',chrome_options=chrome_options)
driver.get('https://www.cnblogs.com/')
print(driver.page_source)

防止检测selenium

#如何规避selenium被检测
from selenium import webdriver
from selenium.webdriver import ChromeOptions
from time import sleep

option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])  # 实验性功能参数

driver = webdriver.Chrome(r'chromedriver.exe',options=option)
driver.get('https://www.taobao.com/')
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值