我爱编程

python-selenium

2018-02-13  本文已影响0人  点点渔火

selenium: 模拟浏览器点击

from selenium import webdriver

self.driver = webdriver.PhantomJS()
self.driver.get(self.url)
self.current_url = self.driver.current_url

模拟登录:

def login(self, login_info):
        self.driver.find_element_by_id("username").send_keys(login_info['user'])
        time.sleep(1) 
                 
        self.driver.find_element_by_id("password"
            ).send_keys(login_info['password'])
        time.sleep(1)
        elem = self.driver.find_element_by_xpath('//*[@id="fm1"]/div/div/div[4]')
        elem.click()
        time.sleep(2)
        if self.current_url != self.driver.current_url:
            print "登录成功"
            self.current_url = self.driver.current_url

页面解析:
解析的函数有很多, 参照w3c的教程

        floder_list = []
        pots = self.driver.find_elements(By.XPATH, '//ul[@id="readTree"]//li[@id="1"]/ul/li[@id="1"]/ul/li[@id]')
        for p in pots:
            floder_list.append([p, 1, 1])  
           # p节点状态都是动态获取的
           pre_class = p.get_attribute("class")   # 点击前的class属性
           p.find_element_by_xpath('span').click()
           prod_class = dt.get_attribute("class")  # 点击后的class属性
上一篇下一篇

猜你喜欢

热点阅读