2017-07-25 33 views
0

我需要迭代组合框中的项目,它不是创建为常规组合框(选择元素),但它是一些“复杂”的JS组件。 我在python中编写了一个while循环来按下按键(以获得另一个项目),检查页面上是否有一些消息,如果消息不在那里,循环应该结束。但它不能正常工作。它不会通过1项,但它似乎走了3项。 (我打印出“真正的/错误的消息”,所以我可以看到,如果它选择了第15项,则只有5条消息来自循环而不是15条) 我不知道如何强制按下按键以缩短时间,只移动一个项目。机器人框架,硒,Python键在循环中的组合框跳过值

功能:

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个项目等。对于他们中的一些有页面上显示的消息,对于一些不是。我需要停止在没有消息,该项目的环...

enter image description here

代码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) 

我会很高兴任何建议如何使其工作。谢谢!

+0

循环将在最坏的情况下运行15次,否则只要'message'为'false',它就会出来。你想达到什么目的? –

+0

我需要检查消息是否出现在页面上的第一个组合框中的每个项目。当第一个项目没有找到消息时结束循环。但是目前它停止在组合框第15项,但实际上,第6和第7项没有消息,所以循环应该在这里结束。甚至在组合框中有更多400个项目的情况下,即使有很多没有消息的项目,它也会继续迭代。 – neliCZka

+0

分享'get_library_instance'和'_is_text_present'的代码 –

回答

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.") 
0

我想改变的条件

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 

应该解决的问题。

+0

它仍然以与之前相同的方式操作:( – neliCZka

+0

需要一段时间才能显示文本,在这种情况之前试着睡一下。 –

+0

是的,我启用了BuiltIn()。在功能中睡眠(2),并且在机器人功能中启用了“设置硒速度.5秒”,所以现在真的很慢,我可以看到它,所以它应该有足够的时间来检查文本...但仍然,像以前一样工作:( – neliCZka