1、在PyCharm中添加selenium:
在命令行中运行
pip install selenium
在PyCharm中settings->Project untitled->Project Interperter,绿色加号添加selenium。
如果安装selenium失败,则修改python中的helpers文件夹下的packging_tool中的do_install和do_uninstall至如下:
def do_install(pkgs): try: # import pip try: from pip._internal import main except Exception: from pip import main except ImportError: error_no_pip() return main(['install'] + pkgs) def do_uninstall(pkgs): try: # import pip try: from pip._internal import main except Exception: from pip import main except ImportError: error_no_pip() return main(['uninstall', '-y'] + pkgs)
2、测试能否使用selenium:
from selenium import webdriver import time brower = webdriver.Firefox() brower.get("http://www.baidu.com") brower.find_element_by_id('kw').send_keys('selenium') brower.find_element_by_id('su').click() time.sleep(3) brower.close()
编译后能否打开指定的网站百度(使用火狐):
如果报错,如下:
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' execut
则需要下载geckodriver.exe(火狐):
下载地址:https://github.com/mozilla/geckodriver/releases,根据自己的电脑,下载的win64位的。
在firefox的安装目录下,解压geckodriver,然后将该路径添加到path环境变量下,不报这个错了;如图: