问题
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 ^^
回答1:
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:
回答2:
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
回答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.
回答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()
回答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()
回答6:
I had the same error.
Web browser was open, username and password were keyed in,but the submit button was not submitted.
I've got the same error, read all solutions and realized that my code was submitted with submit()
driver.find_element_by_id('loginButton').submit()
I changed it to click()
and the issue was solved.
driver.find_element_by_id('loginButton').click()
来源:https://stackoverflow.com/questions/53441658/selenium-in-python-nosuchelementexception-message-no-such-element-unable-to