I tried typing 'abc' in the first block of id and 'cdef' in the second block of password. However, the error code at the bottom comes up.
from selenium import webdriver
driver.get('http://sugang.korea.ac.kr')
Added implicitly wait to prevent the code from executing before the page fully loads.
driver.implicitly_wait(30)
Code for adding username and password is as below
driver.find_element_by_name('id').send_keys('abc')
driver.find_element_by_name('pw').send_keys('cdef')
But getting below error
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"id"}
please. help me ^^
Solution 1
No Such Element Exception usually comes when web driver can't see the element you are trying to perform an action on. Reason's can be:
your ID or Name or Xpath or CssSelector can be wrong.
Your element might be inside an an iframe so that web driver can't see or detect it. Switch to an iframe through Selenium and python
- Your element is taking time to appear on the UI, so you can use an explicit wait to solve this. https://selenium-python.readthedocs.io/waits.html
Solution 2
The username and password fields are within an frame, so you have to:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use the following solution:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By driver = webdriver.Firefox(executable_path=r'C:\\Utility\\BrowserDrivers\\geckodriver.exe') driver.get("http://sugang.korea.ac.kr") WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"firstF"))) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input_login[name='id']"))).send_keys('abc') driver.find_element_by_css_selector("input.input_login[name='pw']").send_keys("cdef")Browser Snapshot:
Solution 3
Add explicitly wait
from selenium.webdriver.support import expected_conditions as EC
userNameElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "id"))
userNameElement.send_keys('abc')
pwdElement= WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.NAME, "pwd"))
pwdElement.send_keys('cdef')
Here, I am expecting that your locators are correct.
Solution 4
It is in a frame which you need to switch to first. Also, use ids where possible as they are faster.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url ="http://sugang.korea.ac.kr"
driver = webdriver.Chrome()
driver.get(url)
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.CSS_SELECTOR,'[name=firstF]')))
driver.switch_to.frame(driver.find_element_by_css_selector('[name=firstF]'))
WebDriverWait(driver,5).until(EC.visibility_of_element_located((By.ID,'id'))).send_keys('abc')
driver.find_element_by_id('pw').send_keys('def')
driver.find_element_by_id('loginButton').click()
Solution 5
The site you are trying to access does not have an element with a tag name id. Examine the site carefully.
<input name="id">
If the input has an id value, try this;
driver.find_element_by_id("id")
Example Use:
HTML:
<div class="form-group">
<input class="form-control" name="username">
</div>
<div class="form-group">
<input class="form-control" name="password" type="password">
</div>
<button id="btn-login" type="submit">Enter</button>
Python:
username = driver.find_element_by_name("username")
password = driver.find_element_by_name("password")
username.send_keys("your_username")
password.send_keys("your_password")
driver.find_element_by_id("btn-login").click()
