简体   繁体   中英

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:

XPath doesn't work on below path code.

HTML

 <span class="voter_details"> <h4 class="v_msg" id="v_msg" style="font-size: 14px;margin-bottom:25px;">Hi, you are here XXXfor in a while, kindly enter your name!</h4> <input value="" placeholder="Enter your Name" type="text" id="v" name="v" class="form-control name_box" maxlength="20"> </span>

Error

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

Code

driver.find_element(driver.find_element(By.XPATH, "//*[@id='v']")).send_keys('random.choice(list)')

Use following xpath to access the input element.

driver.find_element_by_xpath("//input[@placeholder='Enter your Name'][@id='v']").send_keys('Ashwani')

OR You can use WebdriverWait to wait for the element to be clickable and send then enter the value.

WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,"//input[@placeholder='Enter your Name'][@id='v']"))).send_keys('Ashwani')

To execute that you need to have following imports.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

To send a character sequence you you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.name_box#v[placeholder='Enter your Name']"))).send_keys(random.choice(list)) 
  • Using XPATH :

     WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='form-control name_box' and @id='v'][@placeholder='Enter your Name']"))).send_keys(random.choice(list)) 
  • Note : You have to add the following imports :

     from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC 
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="uname"]"}

I got same error while I was working with Selenium. The problem is slow internet/big website. Try adding explicit or implicit delay or you may import time module and add time.sleep before clicking/sending keys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM