Python use Selenium to control the webdriver
来源:互联网 发布:sdn网络控制器 编辑:程序博客网 时间:2024/05/18 22:42
- Summary
- 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.exechrome_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 Truewhile True: if goturl(driver,"http://your url intended to go"): break;#waiting for browser to responsetime.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-8mm = 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 responsetime.sleep(1)
Reference
1,Selenium Installation
2geckodriver download
- Python use Selenium to control the webdriver
- How to use the Segmented Control
- Use Selenium webdriver in Javascript
- Use Python to Drive Selenium RC
- selenium-webdriver(python) (一)
- selenium-webdriver(python) (二)
- selenium-webdriver(python) (三)
- selenium-webdriver(python) (四)
- selenium-webdriver(python) (五)
- selenium-webdriver(python) (六)
- selenium-webdriver(python) (七)
- selenium-webdriver(python) (八)
- selenium-webdriver(python) (九)
- Selenium WebDriver + Python 环境
- selenium webdriver (python)
- selenium webdriver (python)
- selenium-webdriver(python) (十)
- How to use softvol to control the master volume
- 在PostgreSQL中如何生成线性相关的测试数据
- 多线程
- 在socket中使用域名
- 源码安装mysql-5.5.52(centos6.8minimal)
- PhotoView 图片浏览缩放控件
- Python use Selenium to control the webdriver
- C# 委托 从不是创建控件“richTextBox1”的线程访问它。解决方案,利用委托
- MySQL运维(3) 用户管理
- Power Query
- 字符流读取
- 理解Python并发编程一篇就够了 | 进程篇
- JS字符串常用操作
- ubuntu 无法进入图形界面和tty
- Fragment你应该知道的一切