python-unittest 框架利用 PageObject 設計模式在 BasePage 類中呼叫不到 send_keys 方法,執行報錯'NoneType' object has no attribute 'send_keys'
BasePage類程式碼如下:
from selenium.webdriver.support.wait import WebDriverWait as WD
from selenium.webdriver.support import expected_conditions as EC
class BasePage(object):
def init(self, driver):
self.driver = driver
def find_element(self, *loc):
try:
WD(self.driver, 60).until(EC.visibility_of_element_located(*loc))
return self.find_element(*loc)
except:
print("元素沒找到")
def send_keys(self, value, *loc):
self.find_element(*loc).send_keys(value)
第一個問題:上面的BasePage類中的driver是從TestCase類中傳過來的,但是我在寫def send_keys(self, value, *loc)這個方法的時候,寫self.find_element(*loc).send_keys(value)的時候,send_keys(value)這個方法是呼叫不出來的,是不是說明我的driver沒有傳遞過來?如下圖:
HomePage類繼承了BasePage類,程式碼如下:
from basepage import BasePage
from selenium.webdriver.common.by import By
class HomePage(BasePage):
search_input = (By.XPATH, "//div[@id='home-search']//input[@class='typeahead form-control']")
search_button = (By.XPATH, "//div[@id='home-search']//span[@class='icon-search']")
def input_search(self, value):
self.send_keys(value, self.search_input)
def click_search_button(self):
self.find_element(self.search_button).click()
TestCase類的程式碼如下:
import unittest
from selenium import webdriver
from homepage import HomePage
class TestSearch(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.homepage = HomePage(cls.driver)
cls.driver.get("https://support.lenovo.com/us/en")
def test_search(self):
self.homepage.input_search("lenovo")
self.homepage.click_search_button()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
if name == 'main':
unittest.main()
上面的TestCase類中driver通過cls.homepage = HomePage(cls.driver)傳遞給homepage類,然後傳遞給BasePage類中的driver,但是BasePage類中的driver卻呼叫不到 selenium webdriver的一些常用方法,比如send_keys(), page_souce()等,是driver沒有傳遞到BasePage類中嗎?
然後我執行我的TestCase, 果然在這裡 self.find_element(*loc).send_keys(value),報錯了,錯誤如下:
File "D:\automationTestProject\johnunittest\basepage.py", line 20, in send_keys
return self.find_element(*loc).send_keys(value)
AttributeError: 'NoneType' object has no attribute 'send_keys'
小弟以前是學JAVA的,剛剛學習python自動化,請各位大神,指點一下,非常感謝!
相關文章
- 【坑】Tkinter ‘NoneType‘ object has no attribute ‘destroy‘NoneObject
- 問題解決:AttributeError: ‘NoneType‘ object has no attribute ‘append‘ErrorNoneObjectAPP
- Traceback (most recent call last): File "AttributeError: 'NoneType' object has no attribute 'group'ASTErrorNoneObject
- python去掉excel空格和換行,報錯'int' object has no attribute 'strip'PythonExcelObject
- Process object has no attribute '_popen'Object
- ‘dict‘ object has no attribute ‘iteritems‘Object
- Rasa init報錯:AttributeError: type object 'Callable' has no attribute '_abc_registry'ErrorObject
- AttributeError: 'Settings' object has no attribute 'HBase'ErrorObject
- 輸入驗證碼登入時,send_keys () 報錯
- ‘FirewallD‘ object has no attribute ‘path‘ 、‘fw‘、is not registeredObject
- 元素定位突然不能使用了,前面的正常執行,執行 self.driver.find_element (*self.ele.addContent).send_keys (content) 時報錯
- spark-submit執行jar包報錯找不到類的解決方法SparkMITJAR
- Input報錯“Form elements must have labels: Element has no title attribute Element has no placeholde”ORM
- 1_使用face_recognition模組:錯誤AttributeError:'PngStream' object has no attribute 'chunk fcWp'(20190223)ErrorObject
- 在KYLIN中執行查詢報錯
- 聊聊UI自動化的PageObject設計模式UIObject設計模式
- Page Object設計模式Object設計模式
- 在 PO 模式中,driver 如何通過 basePage 在各個 testcase 中傳遞?模式
- idea執行專案報錯找不到jar包IdeaJAR
- JAVA中object類中toString()方法JavaObject
- flink1.14.0在yarn上執行報錯--類載入錯誤--問題Yarn
- performSelector:withObject:afterDelay: 在子執行緒中呼叫不執行performSelectorObject執行緒
- Java中Object類的常用方法JavaObject
- spring配置檔案中配置了,但找不到Bean , 啟動不報錯,執行報錯No bean namedSpringBean
- 複雜網路中louvain演算法實現時報錯AttributeError: module ‘community‘ has no attribute ‘best_partition‘AI演算法ErrorUnity
- web自動化框架—BasePage 類的簡單封裝Web框架封裝
- Laravel 執行 NPM run watch 報錯找不到 cross-envLaravelNPMROS
- 利用SSIS在SQL Azure中執行計劃任務(下)KCSQL
- 利用SSIS在SQL Azure中執行計劃任務(上)DJSQL
- maven打包報錯:在類路徑或引導類路徑中找不到程式包 java.langMavenJava
- JavaBean重寫Object類中的方法JavaBeanObject
- AttributeError: module ‘requests‘ has no attribute ‘_version_‘Error
- AS執行main()方法報錯:SourceSet with name ‘main‘ not foundAI
- 多執行緒設計模式之Worker Thread模式執行緒設計模式thread
- 'This NSPersistentStoreCoordinator has no persistent stores 報錯
- 在Kotlin中如何利用協程進行非同步程式設計Kotlin非同步程式設計
- String類中的equals方法與Object類中的equals方法的不同點Object
- Laravel 中執行 PHP artisan migrate 報錯LaravelPHP