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

Python + Selenium 实现登录Office 365

2014-11-22 11:26 477 查看
最近捡起之前用的Python + Selenium实现工作中需要的登录Office 365功能。(吐槽:国内网络真是卡,登录Office 365实属不易。另外Selenium这样的网站都要墙,无法理解,据说是用了Google的IP,whatever……)

试图研究一下Selenium和WebDriver的关系,看了官方的介绍,先摘录一段有趣的内容:Jump to 2008. The Beijing Olympics mark China’s arrival as a global power,massive mortgage default in the United States triggers the worst internationalrecession since the Great
Depression, The Dark Knight is viewed by every human(twice), still reeling from the untimely loss of Heath Ledger. But the mostimportant story of that year was the merging of Selenium and WebDriver.

我本来想研究当前的Selenium到底哪些是Webdriver哪些是原来的Selenium,但是后来发现两者似乎已经是融为一体的赶脚。可以从他们类的结构窥见一斑:
http://selenium.googlecode.com/git/docs/api/py/api.html
我的登录Office 365的核心代码如下,可以说非常简单:

def login_o365(self):
self.driver = webdriver.Firefox()
self.driver.maximize_window()
self.driver.get(self._login_url)
time.sleep(5)
self.driver.find_element(*self._login_username_field_locator).send_keys(self._login_username)
time.sleep(1)
self.driver.find_element(*self._login_password_field_locator).send_keys(self._login_password)
time.sleep(1)
self.driver.find_element(*self._login_password_field_locator).send_keys(Keys.RETURN)
time.sleep(1)
self.driver.find_element(*self._login_submit_button_locator).click()
time.sleep(20)
print 'Log in O365 successfully!'


其中让我折腾了很久的一个问题是,在输入密码后,一定要有send一个RETURN的步骤,不然点击登录按钮会没有响应,具体原因有待下回分解……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python selenium Office 365