???Python Selenium????????????????
???????????? ???????[ 2014/6/16 14:10:37 ] ????????Python Selenium ????????????
????How to Write Test Code
??????????????????selenium webdriver??????????????????????У?
????from selenium import webdriver
????webdriver????????????е?WebDriver????????????????????selenium webdriver??????????????????????????????
????driver = web driver.Firefox()
????driver.get("http://www.google.com")
???????????Firefox WebDriver???????driver.get?????????????????и?????URL??????????????д???????????????????????????????????????????
????????????????????????????????????????Щ???????ж??
????assert "Google" in driver.title
?????????????ж??????html <title>???????????“Google”????????????????????????????????ж????????????????????檔
????????????????????????????????λ?????????????????Щ?????н???????????????????????????????????????????:
searchInput = driver.find_element_by_id("lst-ib")
searchInput.send_keys("selenium")
searchBtn = driver.find_element_by_name("btnK")
searchBtn.click()
|
????WebDriver?????????λ???????????
????click()??send_keys()????selenium.webdriver.common.action_chains.ActionChains?????????????????????click()?????????????????send_keys()??????????????????Щ??????????????????????????API??
????????????????????????????????driver.close()????driver.quit()?????????????????????close???????tab??quit?????????????????
????A Simple Demo
???????????????????????????????д?????????????????????????????Щ??????????????????????python??????unittest????????д????????????????????????unittest???????????terminal?п?????????檔
import unittest
from selenium import webdriver
class GooglePageTest(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_click_all_links_on_page(self):
driver = self.driver
driver.get("http://www.google.com")
length = len(driver.find_elements_by_tag_name("a"))
for i in range(0??length):
links = driver.find_elements_by_tag_name("a")
if links[i].is_displayed():
links[i].click()
driver.back()
self.assertIn("Google" or "YouTube"??driver.title)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
|
??????δ?????????????unittest????????????????????test case????д?????????????????????Google????????????<a>??????????????????????title?????????????????????????????????????????????terminal?п????????????檔
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11