【问题标题】:Python Selenium send_keys one by onePython Selenium send_keys 一一
【发布时间】:2021-08-02 01:07:12
【问题描述】:
https://www.bccard.com/app/merchant/Login.do

我正在尝试使用 Python-Selenium 自动登录该站点。但是,您可能注意到密码输入位置没有收到driver.send_keys

代码:

from selenium import webdriver
import time

driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe')

driver.get('https://www.bccard.com/app/merchant/Login.do')
time.sleep(2)
alert = driver.switch_to_alert()
alert.dismiss()

driver.find_element_by_xpath('//*[@id="userid"]').send_keys('id')
driver.find_element_by_xpath('//*[@id="passwd"]').send_keys('pw')

【问题讨论】:

  • 您当前的代码到底遇到了什么问题?
  • 运行此代码时,无法将值输入到网站密码表单中
  • 我认为网站禁止了 ctrl-v
  • 我的意思是你收到什么错误?
  • @Robinshim ctrl+v 可能真的被禁止了。检查我的答案。

标签: python selenium selenium-webdriver automation selenium-chromedriver


【解决方案1】:

您的代码对我来说很好,只需切换:

alert = driver.switch_to_alert()

到这里:

alert = driver.switch_to.alert

switch_to_alert() 已被弃用,并且 Pycharm 中出现错误,至少对我而言。

除了用户名和密码填写好

【讨论】:

  • 好吧,我的问题是如何在密码输入时发送密钥
  • 网站的密码输入不支持selenium key_send
  • 我该如何解决我的问题。有没有其他方法可以向网站发送 id 和密码?
  • 我不知道,因为我无法复制您的问题。 send_keys('pw') 对我来说很好,并将 2 个星号 ** 发送到密码字段。祝你好运,希望你能解决。
【解决方案2】:

1 网站在发送一定数量的自动请求后开始阻止这些请求。我添加了一个关于如何避免机器人检测的选项。寻找与此相关的类似问题。

2你可以等你alert并检查里面的文字。

3 不要使用 time.sleep,使用隐式/显式等待 第 2 点和第 3 点是您代码中的主要问题。

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC

options = Options()
#  Try adding user agent to avoid bot detection
# options.add_argument('user-agent=')
driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')
driver.get('https://www.bccard.com/app/merchant/Login.do')
driver.implicitly_wait(10)

wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())
alert = driver.switch_to.alert
assert "INISAFE CrossWebEX" in alert.text
alert.dismiss()

driver.find_element_by_css_selector('li>#userid').send_keys('id')
driver.find_element_by_css_selector('span>#passwd').send_keys('pw')
time.sleep(7)  # just to make sure that the values was entered.

更新和解决方案

我发现您在尝试了一些尝试后开始看到虚拟键盘。所以,你可以用 Javascript 绕过它:

field = driver.find_element_by_css_selector('span>#passwd')
driver.execute_script(f"arguments[0].value='pw'", field)

代替

driver.find_element_by_css_selector('span>#passwd').send_keys('pw')

【讨论】:

    【解决方案3】:

    您可以引入显式等待以提高稳定性。下面的代码对我有用:

    driver = webdriver.Chrome("C:\\Users\\cruisepandey\\Desktop\\Selenium+Python\\chromedriver.exe")
    driver.maximize_window()
    wait = WebDriverWait(driver, 30)
    driver.get("https://www.bccard.com/app/merchant/Login.do")
    alert = driver.switch_to.alert
    alert.dismiss()
    wait.until(EC.element_to_be_clickable((By.ID, 'userid'))).send_keys('Robin shim')
    wait.until(EC.element_to_be_clickable((By.ID, 'passwd'))).send_keys('Robin shim password')
    print("login successfully")
    

    【讨论】:

      猜你喜欢
      • 2018-08-09
      • 2018-06-18
      • 1970-01-01
      • 2016-11-03
      • 2016-05-27
      • 1970-01-01
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多