- 前提条件-先安装好python
D:\now>python Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
- 安装
D:\now>pip install selenium Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting selenium Downloading https://pypi.tuna.tsinghua.edu.cn/packages/80/d6/4294f0b4bce4de0abf13e17190289f9d0613b0a44e5dd6a7f5ca98459853/selenium-3.141.0-py2.py3-none-any.whl (904 kB) |████████████████████████████████| 904 kB 24 kB/s Collecting urllib3 Downloading https://pypi.tuna.tsinghua.edu.cn/packages/0c/cd/1e2ec680ec7b09846dc6e605f5a7709dfb9d7128e51a026e7154e18a234e/urllib3-1.26.5-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 242 kB/s Installing collected packages: urllib3, selenium Successfully installed selenium-3.141.0 urllib3-1.26.5
-
安装驱动
- 检查浏览器版本
-
各浏览器与驱动下载地址
驱动chromedriver:http://chromedriver.storage.googleapis.com/index.html
驱动geckodriver:https://github.com/mozilla/geckodriver/releases
驱动IEDriverServer:http://selenium-release.storage.googleapis.com/index.html
浏览器google:https://www.google.com/chrome/
浏览器firefox:http://ftp.mozilla.org/pub/firefox/releases/
- 下载驱动
- 放到python根目录
-
测试 执行 from selenium import webdriver webdriver.Chrome() 自动启动谷歌浏览器证明安装成功
-
D:\now>python Python 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from selenium import webdriver >>> webdriver.Chrome() DevTools listening on ws://127.0.0.1:54991/devtools/browser/0c068cc8-d4b1-4fe6-88a8-ecc4cfc21c00 [12624:2956:0606/103032.352:ERROR:device_event_log_impl.cc(214)] [10:30:32.352] USB: usb_device_handle_win.cc:1058 Failed to read descriptor from node connection: 连到系统上的设备没有发挥作用。 (0x1F) [12624:2956:0606/103032.371:ERROR:device_event_log_impl.cc(214)] [10:30:32.370] Bluetooth: bluetooth_adapter_winrt.cc:1162 RequestRadioAccessAsync failed: RadioAccessStatus::DeniedByUserWill not be able to change radio power. <selenium.webdriver.chrome.webdriver.WebDriver (session="df68c833ea3780c9380ededbf4a37566")> >>>
-
-
使用
-
#!/usr/bin/python3 from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.baidu.com") # Selenium 的定位元素 # driver.find_element_by_id("su").send_keys("python教程") driver.find_element_by_xpath('//*[@id="su"]').send_keys("python教程")
-
-