【问题标题】:How to iterate over the next element in the map in the inner loop, while coming from the next element of the map in the outer loop?如何在内部循环中迭代地图中的下一个元素,同时来自外部循环中地图的下一个元素?
【发布时间】:2016-09-04 22:36:57
【问题描述】:

我想用旧链接替换新链接,可以是随机的,但两个链接不应该相同。

我写的逻辑是再次遍历内部循环,这可能导致链接设置为与之前的相同。

在函数中,我以字典格式传递值-

${oldLinks}=   Set Variable    {"Facebook":"https://www.facebook.com/","Stack Overflow":"http://stackoverflow.com/"}
${newLinks}=   Set Variable    {"Discovery":"http://www.discovery.com/","HowStuffWorks":"http://www.howstuffworks.com/"}

这是我用python+Robot框架写的函数——

def edit_favorites(d,oldLinks,newLinks):
    oldLinks=json.loads(oldLinks)
    newLinks=json.loads(newLinks)
    BookmarkLinks=oldLinks
    NewBookarks=newLinks
    d.press.back()
    d(hamBergBtn).click()
    d(favorites).click()
    for key,val in BookmarkLinks.iteritems():
        logger.info("Editing the favorites",html=False,also_console=True)
        logger.info("key url"+key,also_console=True)
        # if(d({'textContains':key}))==None:
        #     d({'scrollable':True}).scroll.toEnd()
        for key1,val1 in NewBookarks.iteritems():
            d({'textContains':key}).long_click()
            if d({'textContains':"Copy link URL"}).exists & d({'textContains':"Edit"}).exists & d({'textContains':"Remove"}).exists & d({'textContains':"Cancel"}).exists:
                logger.info("coming here, to edit",also_console=True)
                d({'text':'Edit'}).click()
                d({'resourceId':'com.ex.browser:id/title'}).click()
                if d({"focused":"true"}):
                    d({'resourceId':'com.ex.browser:id/title'}).set_text(key1)
                d({'resourceId':'com.ex.browser:id/address'}).click()
                if d({"focused":"true"}):
                    d({'resourceId':'com.ex.browser:id/address'}).set_text(val1)
                d({'text':'Save'}).click()
                break
            else:
                raise RuntimeError,"Pop up doesn't contain the required fields"

还有其他方法可以实现我想要做的事情吗?

【问题讨论】:

    标签: python loops dictionary iterator logic


    【解决方案1】:

    尝试迭代列表,而不是使用字典。

    from itertools import cycle
    
    li = [link1,link2]
    running = True
    licycle = cycle(li)
    # fetch the next element
    nextelem = licycle.next()
    while running:
      thiselem, nextelem = nextelem, licycle.next()
    

    你可以参考这个-

    Getting the next element while cycling through list

    【讨论】:

    • 我想以键值格式使用它,因为我同时使用键和值。无论如何,感谢您的意见。
    • 在这种情况下,您尝试存储下一个迭代项并尽可能覆盖键、值。
    【解决方案2】:

    你可以试试这个-

    def edit_favorites(d,oldLinks,newLinks):
    oldLinks=json.loads(oldLinks)
    newLinks=json.loads(newLinks)
    OldBookmarkLabel=oldLinks.keys()
    OldBookmarkURL=oldLinks.values()
    NewBookarksLabel=newLinks.keys()
    NewBookarksURL=newLinks.values()
    # d.press.back()
    for i in range(len(OldBookmarkLabel)):
        logger.info("Editing the favorites",html=False,also_console=True)
        logger.info("key url "+OldBookmarkLabel[i],also_console=True)
        # if(d({'textContains':key}))==None:
        #     d({'scrollable':True}).scroll.toEnd()
        d({'textContains':OldBookmarkLabel[i]}).long_click()
        if d({'textContains':"Copy link URL"}).exists & d({'textContains':"Edit"}).exists & d({'textContains':"Remove"}).exists & d({'textContains':"Cancel"}).exists :
            logger.info("coming here, to edit",also_console=True)
            d({'text':'Edit'}).click()
            d({'resourceId':'com.citrix.browser:id/title'}).click()
            if d({"focused":"true"}):
                d({'resourceId':'com.citrix.browser:id/title'}).set_text(NewBookarksLabel[i])
            d({'resourceId':'com.citrix.browser:id/address'}).click()
            if d({"focused":"true"}):
                d({'resourceId':'com.citrix.browser:id/address'}).set_text(NewBookarksURL[i])
            d({'text':'Save'}).click()
         else:
            raise RuntimeError,"Pop up doesn't contain the required fields"
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 2017-01-17
      • 2016-03-06
      • 2011-09-11
      • 1970-01-01
      • 2011-07-03
      • 2016-04-01
      • 2013-05-17
      • 1970-01-01
      相关资源
      最近更新 更多