1

I'm trying to create a script to login into my trading account.

Currently, I am able to click onto the page to login, however the script is unable to locate the username or password input.

I've put a copy of the script below.

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get("https://www.anz.com.au/personal/investing-super/online-share-investing/")

## Clicking on the login button
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.LINK_TEXT, "Log in"))
    )
    element.click()
    print("success in clicking button")

except:
    print("login button has failed")
    driver.quit()
    
## Entering in credentials
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
driver.find_element_by_xpath("//*[@id = 'username']").send_keys("123456")
driver.find_element_by_id("password").send_keys("hello")

# try:
#     # print("switch success")
#     element = WebDriverWait(driver, 10).until(
#         EC.presence_of_element_located((By.ID, "username"))
#     )
#     print("finding element success")
#     element.click()
#     element.send_keys("blanklogin")
  
# except:
#     print("credentials login has failed")
#     driver.quit()

I've tried using both xPath + ID to search but with no luck:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id = 'username']"}

I've also notice that some other users have had problems with iframe's hence tried to include that in the code with no luck. I've also tried clicking on the box first ( in the try-except statement ) but that didn't work either.

Would strongly appreciate any input.

Thanks!!

EDIT: Attaching a screenshot of the HTML in question: html

Sushiix
  • 33
  • 4
  • Can you please provide the HTML page for us to check ? If you can provide the link as well, would be nice since we can check and perform and provide the inputs – Adarsh Kumar GM Aug 12 '21 at 10:13
  • This kind of script could be dangerous though because anyone else might get access to this. – PCM Aug 12 '21 at 10:14
  • @PCM -thanks for the consideration, I have 0 plans of open sourcing this and i'll be trialing this on a dummy account. I want to feel more confident using selenium and thought i'd give this a try :D – Sushiix Aug 12 '21 at 10:25
  • @AdarshKumarGM Yep I can provide the [link](https://www.anz.com.au/personal/investing-super/online-share-investing) to the general page - if you click on the "Log In" button, that is where I am stuck. EDIT: I'll update it with the html ss shortly! – Sushiix Aug 12 '21 at 10:26

2 Answers2

0

try this :

driver = webdriver.Chrome(PATH)

driver.get("https://www.anz.com.au/personal/investing-super/online-share-investing/")

## Clicking on the login button
try:
        btn_rechercher =driver.find_element_by_xpath(".//a[@class='btn btn--blue btn--transparent btn--border-white']")
    
        btn_rechercher.click()
        print("success in clicking button")

except:
    print("login button has failed")
    driver.quit()
    
## Entering in credentials
driver.switch_to.frame(driver.find_element_by_tag_name('iframe'))
driver.find_element_by_xpath("//*[@id = 'username']").send_keys("123456")
driver.find_element_by_id("password").send_keys("hello")
HiFAR
  • 48
  • 1
  • 13
0

When you click on Login button on first page, a new tab opens up. You need to switch to new tab then you can interact with username, password field :

PS : I don't see any iframe in the page, so you do not need to switch to it.

Sample code : -

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)
driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://www.anz.com.au/personal/investing-super/online-share-investing/")
wait = WebDriverWait(driver, 20)

current_handle = driver.current_window_handle
## Clicking on the login button
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.LINK_TEXT, "Log in"))
    )
    element.click()
    print("success in clicking Login in button")

except:
    print("Bot could not click on login button.")
    driver.quit()

all_handles = driver.window_handles
driver.switch_to.window(all_handles[1])

wait.until(EC.element_to_be_clickable((By.ID, "username"))).send_keys('123456')
wait.until(EC.element_to_be_clickable((By.ID, "password"))).send_keys('hello')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.login-button"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
cruisepandey
  • 28,520
  • 6
  • 20
  • 38