简体   繁体   English

Selenium Webdriver:如何在提示符下响应

[英]Selenium Webdriver: how do respond on a prompt

I try to set up a test for a webpage using Selenium WebDriver and Python. 我尝试使用Selenium WebDriver和Python为网页设置测试。 Therefore I start the Docker image selenium/standalone-firefox . 因此,我启动了Docker映像selenium / standalone-firefox

Within this test normally a JavaScript written prompt pops up and want to receive an entry prior I can click OK. 在此测试中,通常会弹出一个JavaScript书面提示,并希望在我单击OK之前接收条目。

But how can I interact with this prompt and the OK button? 但是如何与该提示和“确定”按钮进行交互?

On Selenium IDE the recorder uses answer on next prompt for that. 在Selenium IDE上,刻录机answer on next prompt使用answer on next prompt How to do this with Python-Selenium? 如何使用Python-Selenium做到这一点? If Python does not support an corresponding command, how do I get the needed information to do the same with the available commands? 如果Python不支持相应的命令,如何获得所需的信息以对可用命令执行相同的操作?

在此处输入图片说明

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

from selenium.webdriver.firefox.options import Options

# connect to docker Selenium Server
options = Options()
driver = webdriver.Remote(
   command_executor='http://localhost:4444/wd/hub',
   desired_capabilities=options.to_capabilities()
)

driver.get("https://www.ecalc.ch/motorcalc.php?hacker&lang=en&weight=4500&calc=auw&motornumber=1&warea=60&elevation=300&airtemp=25&motor=hacker&type=2|a60-7xs_v4_28-pole&gear=1&propeller=apc_electric&diameter=18&pitch=10.0&blades=2&batteries=topfuel_light_4500mah_-_30/45c&s=8&esc=master_spin_160_pro&cooling=good")

print(driver.title)

driver.find_element_by_id("modalConfirmOk").click()

driver.find_element_by_name("btnCalculate").click()

driver.find_element_by_id("AddCSV").click()

????

You must handle the prompt alert. 您必须处理提示警报。 Try it with: 尝试使用:

driver.switchTo().alert().sendKeys("Your project name");

You can handle using alert class in selenium. 您可以处理硒中的警报类。

#Switch the control to the Alert window
obj = driver.switch_to.alert

time.sleep(2)

#Enter text into the Alert using send_keys()
obj.send_keys('test')

refer this link selenium 参考此链接硒

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM