【问题标题】:Robot framework,Selenium,Python-key down in loop in combobox skips values机器人框架,Selenium,Python-key 在组合框中循环跳过值
【发布时间】:2017-12-31 06:36:25
【问题描述】:

我需要遍历组合框中的项目,它不是作为常规组合框(选择元素)创建的,但它是一些“复杂”的 JS 组件。 我在python中编写了一个while循环来按下键(以获取另一个项目),检查页面上是否有一些消息,如果消息不存在,则循环应该结束。但它不能正常工作。它没有减少 1 项,但似乎减少了 3 项。 (我打印出“消息 True/False”,所以我可以看到如果它选择第 15 项,则循环中只有 5 条消息而不是 15 条) 我不知道如何强制将键按下更短的时间,仅移动一项。

功能:

    def find_not_used_protocol(self,entity):
    actionChain = self.get_action_chain()
    message = True
    msgs=[]
    while message:
        #actionChain.key_down(Keys.ARROW_DOWN) #I tried this but it did not behave better
        actionChain.send_keys(Keys.DOWN).perform()
        actionChain.release()
        #BuiltIn().sleep(1) #I tried this but it did not behave better
        message = self.get_library_instance()._is_text_present(
            "This protocol already has a "+entity+". Please select different protocol.")
        msgs.append(message) #this is here just for better debug
    return msgs

在机器人中使用:

Set Protocol for ${entity}
    Wait Until Element Is Visible    ${PROTOCOL INPUT}    20
    Input Text   ${PROTOCOL INPUT}    0001
    click element    ${PROTOCOL ARROW DOWN}
    #set selenium speed   .5 seconds  #It does not really help
    ${msgs}=  find not used protocol  ${entity} 
    log to console  ${msgs}

这是组合,它可能有 400 个项目等。其中一些在页面上显示一条消息,而另一些则没有。我需要在没有消息的情况下停止该项目的循环...

get_library_instance 的代码(我的函数):

def get_library_instance(self):
    if self.library is None:
        self.library = BuiltIn().get_library_instance('ExtendedSelenium2Library')
    return self.library

_is_text_present 的代码(来自 Selenium2Library):

def _is_text_present(self, text):
    locator = "xpath=//*[contains(., %s)]" % utils.escape_xpath_value(text);
    return self._is_element_present(locator)

如果有任何建议如何使它发挥作用,我将非常高兴。谢谢!

【问题讨论】:

  • 循环只会在最坏的情况下运行 15 次,否则只要 messagefalse 它就会出来。你想达到什么目标?
  • 我需要检查组合框中每个项目的消息是否显示在页面上,从第一个开始。并在找到第一个没有消息的项目时结束循环。但目前它停在组合框的第 15 项,但实际上第 6 和第 7 项没有消息,所以循环应该到此结束。我什至在组合框中有超过 400 个项目的案例,即使有很多项目没有消息,它也会不断迭代。
  • 分享get_library_instance_is_text_present的代码
  • 在描述中添加
  • 将条件改为message=Please select different protocol

标签: python selenium while-loop automated-tests robotframework


【解决方案1】:

所以终于找到了为什么循环没有按预期运行。 actionChain 确实链接了事件,因此必须在循环之外定义它并在循环中执行。现在它与组合中的 1 件完美搭配。 :)

def find_not_used_protocol(self,entity):
    actionChain = self.get_action_chain()
    actionChain.send_keys(Keys.DOWN)
    actionChain.release()
    message = True
    while message:
        actionChain.perform()
        message = self.is_text_present(
            "This protocol already has a "+entity+". Please select different protocol.")

【讨论】:

    【解决方案2】:

    我想改变条件

    message = self.get_library_instance()._is_text_present(
                "This protocol already has a "+entity+". Please select different protocol.")
    

    message = is_element_visible("//*contains(text(),'Please select different protocol')"]
    

    然后创建一个方法来查看元素是否可见

    def is_element_visible(self, identifier):
        try:
            self.get_library_instance().wait_until_element_is_visible(identifier, 10)
            return True
        except Exception:
            return False
    

    应该可以解决问题。

    【讨论】:

    • 文本是否需要一些时间才能出现,请尝试在此条件之前先休眠。
    • 是的,我在函数中启用了 BuiltIn().sleep(2) 并在机器人函数中启用了“set selenium speed .5 seconds”,所以现在真的很慢,我可以看到它,所以它应该有足够的时间来检查文本......但仍然可以像以前一样工作:(
    • 您是否可以使用以下 xpath 在 firefox 中找到元素(使用 firebug 和 firepath)://*[contains(., 'Please select different protocol.')]
    • 好吧,如果我准确粘贴它会找到 15 个结果...如果我使用 //*[contains(text(),'Please select different protocol.')] 那么它只返回 1 这可能是我们需要的,但这不是 _is_text _present 的实现方式...所以也许我可以创建使用 contains(text()) 的类似功能
    • 是在不同的框架吗?
    猜你喜欢
    • 2016-11-18
    • 2018-08-09
    • 2015-12-08
    • 2020-12-29
    • 1970-01-01
    • 2016-03-15
    • 2020-02-19
    • 2017-04-20
    • 2017-07-02
    相关资源
    最近更新 更多