案例一:网站模拟登录
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
class Douban():
def __init__(self):
self.url = "https://www.douban.com/"
self.driver = webdriver.PhantomJS()
def log_in(self):
self.driver.get(self.url)
time.sleep(3)
self.driver.save_screenshot("0.jpg")
self.driver.find_element_by_xpath('//*[@id="form_email"]').send_keys("xxxxx@qq.com")
self.driver.find_element_by_xpath('//*[@id="form_password"]').send_keys("xxxx")
self.driver.find_element_by_class_name("bn-submit").click()
time.sleep(2)
self.driver.save_screenshot("douban.jpg")
print(self.driver.get_cookies())
def __del__(self):
'''调用内建的稀构方法,在程序退出的时候自动调用
类似的还可以在文件打开的时候调用close,数据库链接的断开
'''
self.driver.quit()
if __name__ == "__main__":
douban = Douban()
douban.log_in()