python selenium webdriver方法封装(find_element_by)
生活随笔
收集整理的這篇文章主要介紹了
python selenium webdriver方法封装(find_element_by)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
下面是對find_element_by_就行了封裝,封裝之后的高級方法就是getElement()
下面是具體的代碼:
def getElement(self, selector):"""to locate element by selector:argselector should be passed by an example with "i,xxx""x,//*[@id='langs']/button":returnsDOM element"""if ',' not in selector:return self.driver.find_element_by_id(selector)selector_by = selector.split(',')[0]selector_value = selector.split(',')[1]if selector_by == "i" or selector_by == 'id':element = self.driver.find_element_by_id(selector_value)elif selector_by == "n" or selector_by == 'name':element = self.driver.find_element_by_name(selector_value)elif selector_by == "c" or selector_by == 'class_name':element = self.driver.find_element_by_class_name(selector_value)elif selector_by == "l" or selector_by == 'link_text':element = self.driver.find_element_by_link_text(selector_value)elif selector_by == "p" or selector_by == 'partial_link_text':element = self.driver.find_element_by_partial_link_text(selector_value)elif selector_by == "t" or selector_by == 'tag_name':element = self.driver.find_element_by_tag_name(selector_value)elif selector_by == "x" or selector_by == 'xpath':element = self.driver.find_element_by_xpath(selector_value)elif selector_by == "s" or selector_by == 'selector_selector':element = self.driver.find_element_by_css_selector(selector_value)else:raise NameError("Please enter a valid type of targeting elements.")return elementdef type(self, selector, text):"""Operation input box.Usage:driver.type("i,el","selenium")"""el = self.getElement(selector)el.clear()el.send_keys(text)以上的代碼是封裝了find_element_by_的幾種方法,在具體使用封裝過的代碼的時候,只需要簡單的調(diào)用即可。
調(diào)用示例
def login(self, userName, password): self.openPage(self.loginPageUrl) # self.driver.clearCookies() self.driver.implicitlyWait(5) self.driver.type("account", userName) self.driver.type("password", password) self.driver.click("submit")自動化的測試代碼示例
def test_ranzhi_login(self): """ 測試用例:測試然之登錄 :return: """ # 新建然之的頁面對象 loginPage = RanzhiSubLoginPage(self.autoDriver, self.baseUrl) # 利用然之的頁面對象進(jìn)行登錄 loginPage.login("admin", "admin") sleep(2) # 斷言 是否登錄成功 self.assertEqual(loginPage.getMainPage(), self.autoDriver.getUrl(), u"登錄失敗") 與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python selenium webdriver方法封装(find_element_by)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Meteor工作目录的划分
- 下一篇: scikit-learn 入门