JavaScript
x
2
1
https://www.bccard.com/app/merchant/Login.do
2
I am trying to login to this site automatically using Python-Selenium. However, as you might noticed the password input place is not receiving driver.send_keys
.
code:
JavaScript
1
13
13
1
from selenium import webdriver
2
import time
3
4
driver = webdriver.Chrome('C:/chromedriver/chromedriver.exe')
5
6
driver.get('https://www.bccard.com/app/merchant/Login.do')
7
time.sleep(2)
8
alert = driver.switch_to_alert()
9
alert.dismiss()
10
11
driver.find_element_by_xpath('//*[@id="userid"]').send_keys('id')
12
driver.find_element_by_xpath('//*[@id="passwd"]').send_keys('pw')
13
Advertisement
Answer
1 The site starts blocking automated requests after some amount of them is sent. I’ve added one option on how to avoid bot detection. Look for similar questions on this.
2 You can wait for you alert and check the text inside it.
3 Do not Use time.sleep, use implicit/explicit wait Points 2 and 3 were the main problems in your code.
JavaScript
1
22
22
1
from selenium import webdriver
2
from selenium.webdriver.support.ui import WebDriverWait
3
from selenium.webdriver.chrome.options import Options
4
from selenium.webdriver.support import expected_conditions as EC
5
6
options = Options()
7
# Try adding user agent to avoid bot detection
8
# options.add_argument('user-agent=')
9
driver = webdriver.Chrome(chrome_options=options, executable_path='/snap/bin/chromium.chromedriver')
10
driver.get('https://www.bccard.com/app/merchant/Login.do')
11
driver.implicitly_wait(10)
12
13
wait = WebDriverWait(driver, 10)
14
wait.until(EC.alert_is_present())
15
alert = driver.switch_to.alert
16
assert "INISAFE CrossWebEX" in alert.text
17
alert.dismiss()
18
19
driver.find_element_by_css_selector('li>#userid').send_keys('id')
20
driver.find_element_by_css_selector('span>#passwd').send_keys('pw')
21
time.sleep(7) # just to make sure that the values was entered.
22
UPDATE and solution
I found out that you start seing a virtual keyboard after some amount of attempts. So, you can bypass it with Javascript:
JavaScript
1
3
1
field = driver.find_element_by_css_selector('span>#passwd')
2
driver.execute_script(f"arguments[0].value='pw'", field)
3
Instead of
JavaScript
1
2
1
driver.find_element_by_css_selector('span>#passwd').send_keys('pw')
2