0
点赞
收藏
分享

微信扫一扫

Python之selenium:selenium库的简介、安装、使用方法之详细攻略


Python之selenium:selenium库的简介、安装、使用方法之详细攻略


目录

​​selenium库的简介​​

​​1、Selenium需要一个驱动程序来与所选的浏览器交互​​

​​selenium库的安装​​

​​selenium库的使用方法​​

​​1、打开一个新的谷歌浏览器或Firefox浏览器​​

​​2、打开一个新的Firefox浏览器,加载百度主页,搜索“seleniumhq”,关闭浏览器​​


selenium库的简介

          Selenium WebDriver的Python语言绑定。selenium包用于从Python自动实现web浏览器交互。安装完成 Selenium 还需要下载一个驱动。


1、Selenium需要一个驱动程序来与所选的浏览器交互

          例如,Firefox需要geckodriver,在运行下面的示例之前需要安装geckodriver。确保它在你的路径中,例如,将它放在/usr/bin或/usr/local/bin中。

如果不遵守这个步骤,将会给你一个错误selenium.common.exceptions.WebDriverException: Message: ' geckodriver '的可执行文件需要在路径中。其他支持的浏览器将有它们自己的驱动程序可用。下面是一些比较流行的浏览器驱动程序的链接


Chrome



​​https://sites.google.com/a/chromium.org/chromedriver/downloads​​



Edge



​​https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/​​



Firefox



​​https://github.com/mozilla/geckodriver/releases​​



Safari



​​https://webkit.org/blog/6900/webdriver-support-in-safari-10/​​



selenium库的安装

pip install selenium

Python之selenium:selenium库的简介、安装、使用方法之详细攻略_chrome


selenium库的使用方法

1、打开一个新的谷歌浏览器或Firefox浏览器

from selenium import webdriver
url='https://www.baidu.com'
driver=webdriver.Chrome(executable_path=r'C:\Users\niu\AppData\Local\Google\Chrome\Application\chrome.exe') # chromedriver.exe
driver.get(url)



from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')


2、打开一个新的Firefox浏览器,加载百度主页,搜索“seleniumhq”,关闭浏览器

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox()

browser.get('http://www.baidu.com')
assert 'Yahoo' in browser.title

elem = browser.find_element_by_name('p') # Find the search box
elem.send_keys('seleniumhq' + Keys.RETURN)

browser.quit()


举报

相关推荐

0 条评论