繁体   English   中英

NoSuchElementException:消息:没有这样的元素:无法定位元素:{“方法”:“名称”,“选择器”:“用户名”},同时向用户名发送文本

[英]NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"username"} while sending text to username

需要帮助解决以下错误:

网址: 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"}

您需要使用 javascript executor 引入 Javascript。 您可以尝试的代码:

只需更改为:

 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")

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

通过使用 ID :

username = driver.find_element_by_id("username");

通过使用名称:

username = driver.find_element_by_name("user_name");

要识别字符序列并将其发送到标有用户名文本的<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")

浏览器快照:

消费税

我更喜欢使用 find_element_by_xpath 因为 xpath 可以很容易地在 chrome 中找到。

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

browser.find_element_by_xpath(xpath)

当然,也有缺点。 当网站更新时,通过 xpath 查找也应该更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“消息:无法找到元素:{“方法”:“名称”,“选择器”:“用户名”}” selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:[name=&quot;username&quot;] NoSuchElementException:消息:没有这样的元素:无法找到元素:{&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;[id=&quot;events_table&quot;]&quot;} selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“.ui流感~”} NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“css selector”,“selector”:“.selected”} NoSuchElementException:消息:无法找到元素:[id =“ j_username”]尝试WebdriverWait NoSuchElementException:消息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id="my id"]"} NoSuchElementException:消息:没有这样的元素:无法定位元素:{“method”:“xpath”,“selector”:“//* [@id =“header3_5”]”} selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{&quot;method&quot;:&quot;xpath&quot;,&quot;selector&quot;:&quot;&quot;} Selenium NoSuchElementException-无法找到元素:{“方法”:“ css选择器”,“选择器”:“ [[名称=”电子邮件地址”]”}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM