最近捡起之前用的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的核心代码如下,可以说非常简单:
1 def login_o365(self): 2 self.driver = webdriver.Firefox() 3 self.driver.maximize_window() 4 self.driver.get(self._login_url) 5 time.sleep(5) 6 self.driver.find_element(*self._login_username_field_locator).send_keys(self._login_username) 7 time.sleep(1) 8 self.driver.find_element(*self._login_password_field_locator).send_keys(self._login_password) 9 time.sleep(1) 10 self.driver.find_element(*self._login_password_field_locator).send_keys(Keys.RETURN) 11 time.sleep(1) 12 self.driver.find_element(*self._login_submit_button_locator).click() 13 time.sleep(20) 14 print 'Log in O365 successfully!'
其中让我折腾了很久的一个问题是,在输入密码后,一定要有send一个RETURN的步骤,不然点击登录按钮会没有响应,具体原因有待下回分解……