NoSuchElementException:消息:没有这样的元素:在向用户名发送文本时,无法找到元素:{“ method”:“ name”,“ selector”:“ username”}

时间:2018-07-23 15:06:51

标签: python selenium selenium-webdriver nosuchelementexception webdriverwait

需要帮助解决以下错误:

URL:https://services.gst.gov.in/services/login

username = driver.find_element_by_name("username");
File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 495, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
    'value': value})['value']
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Users\admin\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"username"}

4 个答案:

答案 0 :(得分:0)

您在查找元素时使用了元素的id值而不是名称值。请在以下任一选项中更改代码

通过使用ID:

username = driver.find_element_by_id("username");

使用名称:

username = driver.find_element_by_name("user_name");

答案 1 :(得分:0)

您需要使用Java脚本执行器介绍Java脚本。您可以试用的代码:

只需更改为:

 driver.execute_script('arguments[0].click();',WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username'))))
 WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys("abc")

完整代码如下:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.action_chains import ActionChains
import time

driver   = webdriver.Chrome(executable_path = r'C:/Users/user**/Downloads/chromedriver_win32/chromedriver.exe')
driver.maximize_window()

wait = WebDriverWait(driver,40)

driver.get("https://services.gst.gov.in/services/login") 

driver.execute_script('arguments[0].click();',wait.until(EC.element_to_be_clickable((By.ID, 'username'))))
wait.until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys("abc")

答案 2 :(得分:0)

要标识字符序列并将其发送到标有文本为用户名<input>字段,您必须诱使 WebDriverWait 为所需的元素可以点击,然后使用execute_script()方法发送文本,如下所示:

  • 代码块:

    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
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://services.gst.gov.in/services/login")
    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.form-control.pad-r-0.ng-pristine.ng-empty.ng-invalid.ng-invalid-required.ng-valid-maxlength.ng-touched#username")))
    driver.execute_script("arguments[0].click();", element)
    element.send_keys("charan teja")
    

浏览器快照:

GST

答案 3 :(得分:0)

我更喜欢使用find_element_by_xpath,因为可以在chrome中轻松找到xpath。

方法如下:右键单击->检查->右键单击->复制-> CopyXpath

browser.find_element_by_xpath(xpath)

当然,有一个缺点。 网站更新后,通过xpath查找也应更新。