码迷,mamicode.com
首页 > 编程语言 > 详细

python selenium

时间:2018-05-13 19:07:34      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:amp   import   htm   sim   close   testing   elf   unit   span   

Example 0:

  • open a new Chrome browser
  • load the page at the given URL
1 from selenium import webdriver
2 
3 browser = webdriver.Chrome()
4 browser.get(http://baidu.com)

 

 

Example 1:

  • open a new Chrome browser
  • load the baidu homepage
  • search for “python”
  • close the browser
 1 from selenium import webdriver
 2 from selenium.webdriver.common.keys import Keys
 3 
 4 browser = webdriver.Chrome()
 5 browser.get(http://www.baidu.com)
 6 
 7 elem = browser.find_element_by_id(kw)
 8 
 9 #Keys.RETURN是导入第二个模块的用法
10 elem.send_keys(python+Keys.RETURN)
11 
12 browser.quit()

 

Example 2:

  Selenium WebDriver is often used as a basis for testing web applications. Here is a simple example uisng Python’s standard unittest library:

 1 import unittest
 2 from selenium import webdriver
 3 from selenium.webdriver.common.keys import Keys
 4 
 5 #该测试类继承自 unittest.TestCase
 6 class PythonOrgSearch(unittest.TestCase):
 7 
 8     def setUp(self):
 9         self.driver = webdriver.Chrome()
10 
11     def test_search(self):
12         driver = self.driver
13         driver.get("http://www.taobao.com")
14         elem = driver.find_element_by_id("q")
15         elem.send_keys("python")
16         elem.send_keys(Keys.RETURN)
17         assert "No results found." not in driver.page_source
18 
19 
20     def tearDown(self):
21         self.driver.close()
22 
23 if __name__ == "__main__":
24     unittest.main()

 

python selenium

标签:amp   import   htm   sim   close   testing   elf   unit   span   

原文地址:https://www.cnblogs.com/sineik/p/9032898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!