执行JS

# -*- coding:utf-8 -*-
"""
执行 js 脚本
"""

from selenium import webdriver
import unittest

class login(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = 'http://www.51testing.com'
        self.verificationErrors = []
        
    def test_loginpass(self):
        driver = self.driver
        driver.get(self.base_url)
        
        js='var q=document.getElementById("username");q.style.border="1px solid red";'
        driver.execute_script(js)
        driver.find_element_by_id('username').send_keys('username')
        driver.find_element_by_id('userpass').send_keys('password')
        driver.find_element_by_id('dologin').click()
        verify = raw_input('输入你看到的验证码,按 Enter 键: ')
        driver.find_element_by_id('xspace-seccode').send_keys(verify)
        driver.find_element_by_id('securitysubmit').click()
        
    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
        
if __name__ == '__main__':
    unittest.main()

 

 1 # -*- coding:utf-8 -*-
 2 """
 3 调用JS代码返回网页的title,并统计超链接的数量
 4 """
 5 
 6 from selenium import webdriver
 7 
 8 driver = webdriver.Firefox()
 9 driver.get("http://www.baidu.com")
10 title = driver.execute_script("return document.title")
11 print title
12 
13 links = driver.execute_script("var links = document.getElementsByTagName('A'); return links.length")
14 print links

 

posted @ 2013-06-13 22:21  Roger|  阅读(208)  评论(0编辑  收藏  举报