Python use Selenium to control the webdriver
2016-12-23 15:05
295 查看
Summary
Install Selenium
Download webdriver
Python scripts
Import selenium webdriver
Connect Chrome Browser
For Firefox case
Goto url address
Input usernamepassword and Login
Reference
For Chrome, it’s “chromedriver.exe”.
For Firefox, no webdriver file required, however you will require to download “geckodriver.exe”, it’s similar to “chromedriver.exe”, otherwise you will encounter below error:
You can refer to this Link to download “geckodriver.exe”.
2geckodriver download
Install Selenium
Download webdriver
Python scripts
Import selenium webdriver
Connect Chrome Browser
For Firefox case
Goto url address
Input usernamepassword and Login
Reference
Summary
Python use Selenium to control the browser is easy to use, and can do lots of stuff, recently used it as automatic login the website and reply the forum post at certain interval.Install Selenium
It’s simple:pip install selenium
Download webdriver
You have to download the webdriver and put somewhere in your computer.For Chrome, it’s “chromedriver.exe”.
For Firefox, no webdriver file required, however you will require to download “geckodriver.exe”, it’s similar to “chromedriver.exe”, otherwise you will encounter below error:
#selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
You can refer to this Link to download “geckodriver.exe”.
Python scripts
Python script is really simple.Import selenium webdriver
from selenium import webdriver
Connect Chrome Browser
#your path to store your chromedriver.exe chrome_path = r"C:\Users\xionghuilin\Desktop\chromedriver.exe" driver = webdriver.Chrome(chrome_path)
For Firefox case
driver = webdriver.Firefox()
Goto url address
def goturl(driver,url): try: driver.get(url) except: return False return True while True: if goturl(driver,"http://your url intended to go"): break; #waiting for browser to response time.sleep(1)
Input username/password and Login
To get the element name, ID or class name, you can right click on the website, then click “Inspect Element”(For Chrome or Firefox).mm = "用户名" #if it is unicode, requires to decode as utf-8 mm = unicode(mm.decode("utf-8")) user=driver.find_element_by_name("element name of the username") user.clear() user.send_keys(mm) password=driver.find_element_by_id("element ID of password") password.send_keys("password") login=driver.find_element_by_class_name("the element on the browser") login.click() #wait for browser to response time.sleep(1)
Reference
1,Selenium Installation2geckodriver download
相关文章推荐
- Mac 软件推荐(续)之程序猿篇
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- Basilisk:一个有着经典的外观和扩展的 Firefox 复刻
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- 把Chrome OS安装在VMWare下及Chrome OS的关键特性和一些截图
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定