您的位置:首页 > 编程语言 > Python开发

python 3 的selenium模块实现网页自动登陆

2017-06-15 14:04 866 查看
基于python 3 的selenium模块实现网页自动登陆

----http://blog.csdn.net/u010637662/article/details/53612197

[Python爬虫] Selenium实现自动登录163邮箱和Locating Elements介绍
----http://blog.csdn.net/eastmount/article/details/47825633

selenium之 chromedriver与chrome版本映射表(更新至v2.30)
----http://blog.csdn.net/huilan_same/article/details/51896672

[Python爬虫] Selenium自动访问Firefox和Chrome并实现搜索截图

----http://blog.csdn.net/eastmount/article/details/47799865

python-selenium-firefox环境搭建时常遇到的小问题
----http://www.cnblogs.com/Jindy-mine/p/6430000.html

firefox 报错“os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH”的解决办法:

1. selenium 3.x开始,webdriver/firefox/webdriver.py的__init__中,executable_path="geckodriver";而2.x是executable_path="wires"
2. firefox 47以上版本,需要下载第三方driver,即geckodriver;在http://docs.seleniumhq.org/download/的Third Party Drivers, Bindings, and Plugins下面找到Mozilla GeckoDriver,下载到任意电脑任意目录,解压后将该路径加入到PC的path(针对windows)即可。

示例:
#!/usr/bin/python
# -*- coding: utf-8 -*-

import time
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

chromedriver = "C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver

driver = webdriver.Chrome(chromedriver)
driver.get("http://172.16.77.160:8080/")
assert "XORMEDIA" in driver.title
user = driver.find_element_by_name("user_id")
user.send_keys("su")
elem_pwd = driver.find_element_by_name("password")
elem_pwd.send_keys("su")
elem_pwd.send_keys(Keys.RETURN)
try:
if driver.find_element_by_id('_PM'):
driver.find_element_by_id('_PM').click()
except:
print('can not find PM')
try:
if driver.find_element_by_xpath('//*[@id="_label"]/tbody/tr[3]/td/a/img'):
driver.find_element_by_xpath('//*[@id="_label"]/tbody/tr[3]/td/a/img').click()
except:
print('can not enter PM')

driver.switch_to_frame('left')
driver.switch_to_frame('leftMainFrame')
try:
if driver.find_element_by_xpath('/html/body/form/table[1]/tbody/tr[3]/td/a'):
driver.find_element_by_xpath('/html/body/form/table[1]/tbody/tr[3]/td/a').click()
except:
print('can not open 电子节目单列表')
print(driver.find_element_by_xpath('/html/body/form/table[1]/tbody/tr[3]/td/a').text)
driver.switch_to_default_content()
driver.switch_to_frame('frameb')
driver.switch_to_frame('mainFrame_b')
try:
if driver.find_element_by_xpath('//*[@id="objSearchCondition"]/tbody/tr[1]/td[2]/select/option[18]'):
driver.find_element_by_xpath('//*[@id="objSearchCondition"]/tbody/tr[1]/td[2]/select/option[18]').click()
except:
print('can not open CCTV1')
driver.find_element_by_xpath('//*[@id="objSearchCondition"]/tbody/tr[1]/td[4]/a').click()

assert "test" in driver.title
driver.close()
driver.quit()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  selenium python