TheRayArmy
TheRayArmy

Reputation: 11

Python Selenium Unable to locate element

I'm trying to make a Python program that will automate login to my school's website. However, I'm returned with an error:

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

The relevant code section throwing the error is:

BCnumber = driver.find_element_by_xpath('//*[@id="loginidtext"]')
BCnumber.send_keys('loginid')

The website is: https://matrix.tjc.edu.sg/?topleft=toprow.php&bottomright=bottomrow.php

I've tried using:

driver.switch_to 

to switch to the relevant div but the same error was returned...

Upvotes: 1

Views: 1869

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

Here is the Answer to your Question:

As the locator //*[@id="loginidtext"] is within topwindow iframe, we have to switch over to the iframe first as follows:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get('https://matrix.tjc.edu.sg/?topleft=toprow.php&bottomright=bottomrow.php')
driver.maximize_window()
driver.implicitly_wait(20)
driver.switch_to.frame("topwindow")
BCnumber = driver.find_element_by_xpath('//*[@id="loginidtext"]')
BCnumber.send_keys('loginid')

Let me know if this Answers your Question.

Upvotes: 1

Related Questions