Python硒send_keys一一
Python Selenium send_keys one by one
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')
你的代码对我来说工作正常,只需切换:
alert = driver.switch_to_alert()
为此:
alert = driver.switch_to.alert
switch_to_alert()
已弃用,并且在 Pycharm 中出现错误,至少对我而言是这样。
除了UserID和Password都填好了
您可以引入显式等待以提高稳定性。以下代码对我有用:
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")
1 该站点在发送一定数量的自动请求后开始阻止它们。我添加了一个关于如何避免机器人检测的选项。寻找类似的问题。
2 等你提醒,查看里面的文字。
3 不要使用time.sleep,使用implicit/explicit等待
第 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')
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')
你的代码对我来说工作正常,只需切换:
alert = driver.switch_to_alert()
为此:
alert = driver.switch_to.alert
switch_to_alert()
已弃用,并且在 Pycharm 中出现错误,至少对我而言是这样。
除了UserID和Password都填好了
您可以引入显式等待以提高稳定性。以下代码对我有用:
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")
1 该站点在发送一定数量的自动请求后开始阻止它们。我添加了一个关于如何避免机器人检测的选项。寻找类似的问题。
2 等你提醒,查看里面的文字。
3 不要使用time.sleep,使用implicit/explicit等待 第 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')