获取所有链接并遍历列表 (机器人框架)

2024-10-01 15:32:44 发布

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

我想检查一些收件箱中的所有项目,然后一个一个地打开,直到没有更多的项目(将对每个项目进行一些处理,因此它们在被访问后会消失)。这些项目是基于工作流的,因此当它打开时,它们将被处理并从收件箱中消失。在

如何让robot列出这些项目并逐一打开?这样做是正确的吗?我在eclipsewpython中使用robotframework

//span/a将捕获收件箱(10)enter image description here中的所有相关链接

我有下面的代码,它获取所有链接并将它们放入一个列表中,但我不知道什么是最好的方法是逐个打开它们(每次打开链接并完成一些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}

Tags: of项目loggetindex链接countlinks
1条回答
网友
1楼 · 发布于 2024-10-01 15:32:44

如果您想返回到您所在的页面,您可以在单击链接之前获取位置,然后在处理后返回到该url。你甚至不需要为此创建一个链接测试列表。对于xpath,将其设为//a/span[string-length(text())>1],这样就不再需要run keyword 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发生了变化,那么您只需通过之前的操作再次返回该页面。在

相关问题 更多 >

    热门问题