python-selenium

作者: 点点渔火 | 来源:发表于2018-02-13 15:18 被阅读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属性

相关文章

网友评论

    本文标题:python-selenium

    本文链接:https://www.haomeiwen.com/subject/wlgrwxtx.html