1、安装Chrome浏览器
2、下载安装Selenium
# 升级:
pip install -U selenium
# 源码安装:
python setup.py install
3、下载对应浏览器对应版本的chromedriver,放到Chrome安装目录下。
ChromeDriver版本:官方旧版汇总,本地;官方新版下载,本地,下载列表
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('user-agent="Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19"')
browser = webdriver.Chrome("E:\Program Files\Chrome53_goa_v2017.1.2\Browser\chromedriver.exe", chrome_options = chrome_options)
browser.get('https://www.baidu.com/')
print(browser.title)
page = driver.find_elements_by_xpath("//div[@class='pagexxx']")
driver.execute_script('arguments[0].scrollIntoView();', page[-1]) #拖动到可见的元素去
nextpage = driver.find_element_by_xpath("//a[@data-fun='next']")
nextpage.click()
browser.quit()
Python language bindings for Selenium WebDriver.
The selenium package is used to automate web browser interaction from Python.
| Home: | http://www.seleniumhq.org |
|---|---|
| Docs: | selenium package API |
| Dev: | https://github.com/SeleniumHQ/Selenium |
| PyPI: | https://pypi.python.org/pypi/selenium |
| IRC: | #selenium channel on freenode |
Several browsers/drivers are supported (Firefox, Chrome, Internet Explorer, PhantomJS), as well as the Remote protocol.
If you have pip on your system, you can simply install or upgrade the Python bindings:
pip install -U selenium
Alternately, you can download the source distribution from PyPI (e.g. selenium-3.4.1.tar.gz), unarchive it, and run:
python setup.py install
Note: both of the methods described above install selenium as a system-wide package That will require administrative/root access to their machine. You may consider using a virtualenv to create isolated Python environments instead.
Selenium requires a driver to interface with the chosen browser. Firefox, for example, requires geckodriver, which needs to be installed before the below examples can be run. Make sure it’s in your PATH, e. g., place it in /usr/bin or /usr/local/bin.
Failure to observe this step will give you an error selenium.common.exceptions.WebDriverException: Message: ‘geckodriver’ executable needs to be in PATH.
Other supported browsers will have their own drivers available. Links to some of the more popular browser drivers follow.
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('http://seleniumhq.org/')
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('http://www.yahoo.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()
Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example uisng Python’s standard unittest library:
import unittest
class GoogleTestCase(unittest.TestCase):
def setUp(self):
self.browser = webdriver.Firefox()
self.addCleanup(self.browser.quit)
def testPageTitle(self):
self.browser.get('http://www.google.com')
self.assertIn('Google', self.browser.title)
if __name__ == '__main__':
unittest.main(verbosity=2)
For normal WebDriver scripts (non-Remote), the Java server is not needed.
However, to use Selenium Webdriver Remote or the legacy Selenium API (Selenium-RC), you need to also run the Selenium server. The server requires a Java Runtime Environment (JRE).
Download the server separately, from: http://selenium-release.storage.googleapis.com/3.4/selenium-server-standalone-3.4.0.jar
Run the server from the command line:
java -jar selenium-server-standalone-3.4.0.jar
Then run your Python client scripts.
View source code online:
official: https://github.com/SeleniumHQ/selenium/tree/master/py