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

Python之selenium:selenium库的简介、安装、使用方法之详细攻略selenium库的简介Selenium WebDriver的Python语言绑定。selenium包用于从Python自动实现web浏览器交互。安装完成 Selenium 还需要下载一个驱动。1、Selenium需要一个驱动程序来与所选的浏览器交互例如,Firefox需要geckodriver,在运行下面的示例之前需要安装geckodriver。确保它在你的路径中,例如,将它放在/usr/bin或/usr/local/bin中。如果不遵守这个步骤,将会给你一个错误selenium.common.exceptions.WebDriverException: Message: ' geckodriver '的可执行文件需要在路径中。其他支持的浏览器将有它们自己的驱动程序可用。下面是一些比较流行的浏览器驱动程序的链接。Chromehttps://sites.google.com/a/chromium.org/chromedriver/downloadsEdgehttps://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/Firefoxhttps://github.com/mozilla/geckodriver/releasesSafarihttps://webkit.org/blog/6900/webdriver-support-in-safari-10/selenium库的安装pip install selenium

selenium库的使用方法1、打开一个新的谷歌浏览器或Firefox浏览器from selenium import webdriverurl='https://www.baidu.com'driver=webdriver.Chrome(executable_path=r'C:\Users\niu\AppData\Local\Google\Chrome\Application\chrome.exe')   #  chromedriver.exedriver.get(url)from selenium import webdriverbrowser = webdriver.Firefox()browser.get('http://seleniumhq.org/') 2、打开一个新的Firefox浏览器,加载百度主页,搜索“seleniumhq”,关闭浏览器from selenium import webdriverfrom selenium.webdriver.common.keys import Keysbrowser = webdriver.Firefox()browser.get('http://www.baidu.com')assert 'Yahoo' in browser.titleelem = browser.find_element_by_name('p')  # Find the search boxelem.send_keys('seleniumhq' + Keys.RETURN)browser.quit()

(0)

相关推荐