selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

前端 未结 6 713
甜味超标
甜味超标 2020-11-22 08:41

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          


        
相关标签:
6条回答
  • 2020-11-22 09:18

    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:

    1. your ID or Name or Xpath or CssSelector can be wrong.

    2. 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

    3. 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
    0 讨论(0)
  • 2020-11-22 09:19

    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()
    
    0 讨论(0)
  • 2020-11-22 09:24

    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:

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-22 09:28

    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()
    
    0 讨论(0)
  • 2020-11-22 09:37

    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.

    0 讨论(0)
提交回复
热议问题