如何在内部循环中迭代映射中的下一个元素,同时从外部循环中映射的下一个元素?

2024-06-16 11:11:51 发布

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

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

我编写的逻辑是再次迭代内部循环,这可能导致链接设置为与前一个相同。在

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

${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框架编写的函数-

^{pr2}$

有没有其他的方法可以达到我想要做的事情?在


Tags: 函数httpscomhttpfacebook字典stack链接
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"

不要使用字典,尝试迭代列表。在

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

相关问题 更多 >