Selenium webdriver 如何测试文本字段中的占位符

Selenium webdriver 如何测试文本字段中的占位符,selenium-webdriver,robotframework,Selenium Webdriver,Robotframework,我想使用robotframework验证文本字段中存在的占位符文本 我使用了不同的Selenium2库关键字,但它们都不是我想要的 有人有办法从我的测试中获取此功能吗?您可以运行命令getAttributeinput\u字段_locator@placeholder. 这将返回您想要的文本,然后您可以对其进行断言 一旦您成功地从REPL访问了您正在寻找的功能,您可能会发现它无法通过robotframework访问。您可以通过为Selenium2Library创建一个包装器来公开新的关键字,该包装器

我想使用robotframework验证文本字段中存在的占位符文本

我使用了不同的Selenium2库关键字,但它们都不是我想要的


有人有办法从我的测试中获取此功能吗?

您可以运行命令getAttributeinput\u字段_locator@placeholder. 这将返回您想要的文本,然后您可以对其进行断言

一旦您成功地从REPL访问了您正在寻找的功能,您可能会发现它无法通过robotframework访问。您可以通过为Selenium2Library创建一个包装器来公开新的关键字,该包装器通过额外的功能对其进行了扩展——例如,请参见我正在编写的教程中的一个示例

如果您改为导入此类Get Text和Get HTML,则此示例仅将两个关键字添加到robotframework中Selenium2Library中的关键字之上,这两个关键字对于验证非常有用:

from Selenium2Library import Selenium2Library
class Selenium2Custom(Selenium2Library):
    """
    Custom wrapper for robotframework Selenium2Library to add extra functionality
    """
    def get_text(self, locator):
        """
        Returns the text of element identified by `locator`.

        See `introduction` for details about locating elements.
        """
        return self._get_text(locator)
    def get_html(self, id=None):
        """
        Get the current document as an XML accessor object.
        """
        from lxml import html
        src = self.get_source().encode('ascii', 'xmlcharrefreplace')
        page = html.fromstring(src)
        element = page.get_element_by_id(id) if id is not None else page
        return html.tostring(element)
因此,做这样的事情是微不足道的:

from Selenium2Library import Selenium2Library
class Selenium2Custom(Selenium2Library):
    """
    Custom wrapper for robotframework Selenium2Library to add extra functionality
    """
    def get_placeholder(self, locator):
        """
        Returns the placeholder text of element identified by `locator`.
        """
        element = self._element_find(locator, True, False)
        return element.get_attribute("@placeholder")
现在我不知道这是否一定会对你起作用,但对我来说,它的作用是这样的:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from Selenium2Library import Selenium2Library
>>> def get_placeholder(self, locator):
...     element = self._element_find(locator, True, False)
...     return element.get_attribute("placeholder")
... 
>>> Selenium2Library.get_placeholder = get_placeholder
>>> session = Selenium2Library()
>>> session.open_browser("http://www.wikipedia.org/wiki/Main_Page",
                         remote_url="http://127.0.0.1:4444/wd/hub")
1
>>> session.get_placeholder("search")
u'Search'
>>> 

我还想检查占位符文本,在来到这里并阅读评论后,我得到了一些关于它的线索。现在,棘手的部分是如何在robot框架中实现这一点,在做了一些数学运算之后,我能够非常轻松地做到这一点。以下是我用两行文字给出的答案:

${webelement}=  Get WebElement  locator

${placeholder}=  Call Method  ${webelement}  get_attribute  placeholder

我猜您已经尝试过getValue,我可以检查一下您是在使用selenium还是selenium 2吗?您的python知识水平如何?我经常发现打开REPL并尝试手动运行每个步骤以收集信息非常有用,a可能您需要的东西不在关键字库中,因此您可能需要编写一个扩展。@BiggAl。是的,我尝试过GetValue,页面应该包含关键字。我正在使用Selenium2,我对python编码很满意,但还没有为此尝试过任何代码。如果您有任何建议,请与我们分享。我将在REPL导入中尝试Selenium2库,然后在您选择的文本编辑器中打开源代码-您应该能够找到s2l如何调用selenium,并在REPL中创建一个运行getAttribute的函数,如srkgupta的示例中所示-运行此函数将为您提供您想要的结果对此评论的回复,我将编写一个关于如何扩展s2l的完整答案对于您的测试,这是您将为香草硒所做的,太多了一点,将其连接到Robotframework将需要额外的努力-请参阅我的答案以了解操作方法