Skip to content

入门

http://www.selenium.org.cn/

https://selenium-python-zh.readthedocs.io/en/latest/index.html

pip install selenium

chromedriver

查看chrome版本

http://npm.taobao.org/mirrors/chromedriver/

下载对应版本的chromedriver

1
2
3
➜  ~ cd /usr/local
➜  local sudo mkdir chromedriver
➜  Downloads sudo cp chromedriver /usr/local/chromedriver

.zshrc添加export PATH="$PATH:/usr/local/chromedriver"

1
2
3
4
5
6
source .zshrc

➜  ~ chromedriver
Starting ChromeDriver 77.0.3865.40 (f484704e052e0b556f8030b65b953dce96503217-refs/branch-heads/3865@{#442}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.

GeckoDriver

https://github.com/mozilla/geckodriver

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

1
sudo mv geckodriver /usr/local/bin

简单示例

1
2
3
from selenium import webdriver

brower = webdriver.Chrome()

执行上面的代码,如果弹出一个空白的 Chrome 浏览器,则证明所有的配置都没有问题

使用brower.quit()即可退出

登陆三国杀填写账号密码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
from selenium import webdriver
import time

brower = webdriver.Chrome()
brower.get('http://wanba.baidu.com/games/sgs/')
time.sleep(2)

login_button = brower.find_element_by_xpath('//form//button')

login_button.click()
time.sleep(1)

input_username = brower.find_element_by_xpath('//input[@placeholder="手机/邮箱/用户名"]')
input_username.send_keys('wodezhanghao')

input_password = brower.find_element_by_xpath('//input[@placeholder="密码"]')
input_password.send_keys('wodemima')