[Python] [Selenium]如何在处理Firefox浏览器警报时切换输入字段? [英] [Python][Selenium] How to switch input fields while handling firefox browser alerts?

查看:169
本文介绍了[Python] [Selenium]如何在处理Firefox浏览器警报时切换输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个密码保护的代理,其用户名和密码不断变化,所以我需要在启动浏览器时显示的firefox提示符中始终输入。我无法找到从用户名输入切换到密码输入。我已经使用这段代码来处理警报:

  try:
WebDriverWait(driver,3).until (EC.alert_is_present(),
'等待PA创建'+
'确认弹出窗口出现。')

alert_input = Alert(driver)
alert_input.send_keys('username')
alert_input.send_keys(Keys.TAB)
alert_input.send_keys('password')
alert_input.accept()
print(alert accepted )
除了TimeoutException:
print(no alert)

,这是行不通的,因为在用户名字段中输入用户名后,用代替,而不移动到密码字段。所以我假设的是,它实际上进入到用户名字段的标签,而不是标签到密码字段。有没有一种方法来切换字段没有Tab键或以某种方式与Tab键做?



谢谢

解决方案

而不是发送tab键到浏览器,您可以通过id名称或xpath获取用户名和密码元素,并发送输入。
这里是例子:

$ p $ username = driver.find_element_by_name(用户名字段名称)
username.send_keys(username)
password = driver.find_element_by_name(密码字段的名称)
password.send_keys(password)


I have a password protected proxy whose username and password constantly change so I need to always enter them in the firefox prompt that shows when you start the browser. I can't find anyway to switch from the username input to the password input. I've been using this code to handle the alert:

try:
    WebDriverWait(driver, 3).until(EC.alert_is_present(),
                                   'Timed out waiting for PA creation ' +
                                   'confirmation popup to appear.')

    alert_input = Alert(driver)
    alert_input.send_keys('username')
    alert_input.send_keys(Keys.TAB)
    alert_input.send_keys('password')
    alert_input.accept()
    print("alert accepted")
except TimeoutException:
    print("no alert")

However, this doesn't work because after entering the username into the username field, it replaces it with  and doesn't move to the password field. So what I'm assuming is that it literally enters the tab into the username field instead of tabbing to the password field. Is there a way to switch fields without the tab key or somehow do it with the tab key?

Thanks

解决方案

rather than sending tab key to the browser you can get username and password element by id name or xpath and send input. Here is the example:

username = driver.find_element_by_name("name of the username field")
username.send_keys("username")
password = driver.find_element_by_name("name of the password field")
password.send_keys("password")

这篇关于[Python] [Selenium]如何在处理Firefox浏览器警报时切换输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆