如何在python脚本中使用squish工具从datalist获取选项和值

2024-06-16 11:54:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在自动化一个web应用程序。我必须单击一个文本框,然后从单击文本框后显示的列表框中选择一个值。请看所附图片 enter image description here

这是我写的代码:

    childObjOfParentDiv= getChildrenObject(eval(parentObj))        
    selectOptionFromList(childObjOfParentDiv[1],"JACK")
@staticmethod    
    def getChildrenObject(parentObject):
      children = []
      try:
          parentObject = Synchronization.waitForObject(parentObject) 
          for child in object.children(parentObject):
              children.append(child)
      except Exception as err:
          Logger.error("exception message: %s, exception args: %s", err.message, err.args)
      finally:
          return children

@staticmethod
    def selectOptionFromList(parentObject, optionValue, propertyName = 'value', exactMatch = False):

        children = Utilities.getChildrenObject(parentObject)
        for child in children:
            try:
                propertyValue = Utilities.getObjectPropertyValue(child, propertyName).strip()
                if exactMatch and (optionValue == propertyValue):                      
                    Wrapper.mouseClick(child)
                    Synchronization.wait(StaticWait.minWaitInSeconds)
                    break                    
                elif optionValue in propertyValue:                      
                    Wrapper.mouseClick(child)
                    Synchronization.wait(StaticWait.minWaitInSeconds)
                    break
            except Exception as err:
                Logger.error("exception message: %s, exception args: %s", err.message, err.args)

但是datalist中的选项没有被选中,datalist显示为空


Tags: inchildmessageexceptionargserrsynchronization文本框
1条回答
网友
1楼 · 发布于 2024-06-16 11:54:01

下面是一个例子。你知道吗

请注意,输入要选择的datalist条目文本是必需的,因为至少Firefox似乎没有报告datalist选项元素的有效屏幕坐标,并且调用option元素的click方法似乎没有效果。你知道吗

在输入元素中输入条目文本后,数据列表将向下过滤到该条目,然后可以通过相对于输入元素的鼠标单击或按键来选择条目。你知道吗

def main():
    startBrowser("https://davidwalsh.name/demo/datalist.php")

    o = waitForObject({"name": "frameworks", "tagName": "INPUT", "type": "text", "visible": True})
    select_datalist_entry(o, "jquery")
    snooze(2)


def select_datalist_entry(input_obj, datalist_entry_text):
    nativeMouseClick(input_obj, MouseButton.LeftButton)
    snooze(0.5)
    nativeType("<Ctrl+a>")
    nativeType("<Delete>")
    snooze(0.5)
    nativeMouseClick(input_obj, MouseButton.LeftButton)
    snooze(0.5)
    nativeType(datalist_entry_text)
    snooze(0.5)
    x = input_obj.x + 5
    y = input_obj.y + (2 * input_obj.height)
    nativeMouseClick(x, y, MouseButton.LeftButton)

    # Alternative approach to select from data list:
    #nativeType("<Down>")
    #snooze(0.5)
    #nativeType("<Return>")

相关问题 更多 >