2017-07-24 107 views
1

我是新來robotframeworkPython的RobotFramework查找元素

我想做出一些功能不在標準selenium2library

1) 如何讓司機在我的函數使用?

2) 如何在庫中使用_element_find函數(來自Selenium2Library/keywords/_element.py)? (我試圖導入大多一切,仍然_element_find是遙不可及)

class page(object): 
    def __init__(self, driver=None, title=None, url=None): 
     self._driver = driver 
     self._title = title 
     self._url = url 

def get_driver(self): 
    return self._driver 

def wait_for_visibility(self, locator, info="no error", timeout=10): 
    return WebDriverWait(self.get_driver(), timeout).until(
     expected_conditions.visibility_of_element_located(locator), info) 

def find_element(self, locator): 
    return self.get_driver().find_element(*locator) 

def clear_field(self, locator): 
    self.find_element(locator).clear() 

def send_keys(self, value_to_send, locator, info="field was not visible"): 
    self.wait_for_visibility(locator, info) 
    self.find_element(locator).send_keys(value_to_send) 
    return self 

def clear_field_and_send_keys(self, value_to_send, locator, info="field was not visible"): 
    self.clear_field(locator) 
    self.send_keys(value_to_send, locator, info) 

def send_to_field_random_value_of_length(self, locator, leng, info="field was not visible"): 
    self.clear_field(locator) 
    value = rstr.rstr("abcdefghijklmnoprstuwxyz", leng) 
    self.send_keys(value, locator, info) 

def my_click(self, locator, info="click on button error", timeout=5): 
    element = self.wait_for_visibility(locator, info, timeout) 
    element.click() 

話,我想用它來做例子是這樣的:

Register Proper Data 
    [Setup] Open Browser ${web-page} browser=${browser} 
    my click (By.PARTIAL_LINK_TEXT, "Zarejestruj nowe konto") 
    send to field random value of length (By.ID, "rejestracja_konta_imie") 7 

是適當的解決辦法?

現在我得到錯誤AttributeError的:「NoneType」對象有沒有屬性「find_element」

回答

2

我找到了答案

from Selenium2Library import Selenium2Library 

class page(Selenium2Library): 

def get_driver(self): 
    return self._current_browser() 

的錯誤,現在我越來越: Python Robot Framework Pass arguments to a function 誰能幫助?

+0

好。你使用了繼承。我建議重命名班。 Python類應該以大寫字母開頭。我試圖編輯這個,但「編輯必須至少有6個字符」。多麼奇怪的要求。 –

1

你的錯誤可能是從return self.get_driver().find_element(*locator)在find_element到來。

嘗試創建一個最小的工作示例並共享,以便我們可以重現。

2

在Robot Framework之外使用Selenium2Library的目的是什麼?如果你創建自己的Python庫,你應該使用純硒Webdriver。爲了給現有的Selenium2Library增加一些功能,你應該擴展這個類並創建包含新方法的你自己的(例如JedrekSelenium2Library)。