我想检查某个收件箱中的所有项目,然后将它们一一打开,直到没有更多项目为止(将对每个项目进行一些处理,因此它们在被访问后就会消失)。这些项目是基于工作流程的,因此当它打开时,它们将被处理并从收件箱中消失。
如何让机器人列出这些项目并一一打开它们?这是完成此类任务的正确方法吗?我在 Eclipse 和 Python 中使用 RobotFramework
我有以下代码,它获取所有链接并将它们放入列表中,但我不知道逐一打开它们的最佳方法是什么(每次打开链接后返回收件箱并且某些 X 进程已已完成)。
Wait Until Element is Visible xpath=//a/span
# Count number of links on page
${AllLinksCount}= Get Matching Xpath Count xpath=//a/span
# Log the count of links
Log ${AllLinksCount}
# Create a list to store the link texts
@{LinkItems} Create List
# Loop through all links and store links value that has length more than 1 character
: FOR ${INDEX} IN RANGE 1 ${AllLinksCount}
\ Log ${INDEX}
\ ${lintext}= Get Text xpath=(//a/span)[${INDEX}]
\ Log ${lintext}
\ ${linklength} Get Length ${lintext}
\ Run Keyword If ${linklength}>1 Append To List ${LinkItems} ${lintext}
${LinkSize}= Get Length ${LinkItems}
Log ${LinkSize}
Comment Print all links
: FOR ${ELEMENT} IN @{LinkItems}
\ Log ${ELEMENT}
最佳答案
如果你想回到原来的页面,你可以在点击链接之前获取位置,然后在处理后返回到该网址。您甚至不需要为此创建链接测试列表。对于您的 xpath,将其设为 //a/span[string-length(text())>1]
,这样您就不需要 run 关键字 if
关键字不再:
Wait Until Element is Visible
xpath=//a/span[string-length(text())>1]
${AllLinksCount}= Get Matching Xpath Count xpath=//a/span[string-length(text())>1]
Log ${AllLinksCount}
: FOR ${INDEX} IN RANGE 1 ${AllLinksCount}
\ Log ${INDEX}
\ ${currUrl} get location
\ click element xpath=(//a/span[string-length(text())>1])[1]
\ do processing here
\ go to ${currUrl}
如果 URL 发生变化,您只需通过之前的方式再次返回该页面即可。
关于python - 获取所有链接并迭代列表(机器人框架),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41181087/