在 Robot Framework 中处理浏览器弹出窗口(如 alert、confirm、prompt 等)通常使用 SeleniumLibrary 提供的关键字。以下是一些常见的弹出窗口类型及其处理方法:
Alert 弹出窗口通常只包含一个确认按钮。
Alert Should Be Present
Get Alert Message
Dismiss Alert
Accept Alert
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} http://example.com/alert
*** Test Cases ***
Handle Alert
Open Browser ${URL} chrome
Click Button id=alertButton
Alert Should Be Present
${alert_message}= Get Alert Message
Log Alert message: ${alert_message}
Accept Alert
Close Browser
Confirm 弹出窗口包含确认和取消按钮。
Alert Should Be Present
Get Alert Message
Dismiss Alert
Accept Alert
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} http://example.com/confirm
*** Test Cases ***
Handle Confirm
Open Browser ${URL} chrome
Click Button id=confirmButton
Alert Should Be Present
${confirm_message}= Get Alert Message
Log Confirm message: ${confirm_message}
# Accept the confirm dialog
Accept Alert
# Or dismiss the confirm dialog
# Dismiss Alert
Close Browser
Prompt 弹出窗口包含一个输入框和确认、取消按钮。
Alert Should Be Present
Get Alert Message
Input Text Into Alert
Dismiss Alert
Accept Alert
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} http://example.com/prompt
*** Test Cases ***
Handle Prompt
Open Browser ${URL} chrome
Click Button id=promptButton
Alert Should Be Present
${prompt_message}= Get Alert Message
Log Prompt message: ${prompt_message}
Input Text Into Alert My input text
Accept Alert
# Or dismiss the prompt dialog
# Dismiss Alert
Close Browser
文件上传弹出窗口通常需要直接与文件输入元素交互。
Choose File
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} http://example.com/upload
${FILE_PATH} C:/path/to/your/file.txt
*** Test Cases ***
Handle File Upload
Open Browser ${URL} chrome
Choose File id=fileInput ${FILE_PATH}
Click Button id=uploadButton
Close Browser
有时,点击某个链接会打开一个新的浏览器窗口或标签页。
Wait Until Number Of Windows Is
Switch Window
Close Window
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${URL} http://example.com
*** Test Cases ***
Handle New Window
Open Browser ${URL} chrome
Click Link id=newWindowLink
Wait Until Number Of Windows Is 2
Switch Window NEW
# Perform actions in the new window
Log Current URL: ${URL}
Close Window
Switch Window MAIN
Close Browser
领取专属 10元无门槛券
手把手带您无忧上云