Python NoSuchElementException:消息:没有这样的元素:在尝试查找或访问元素标记时无法定位元素
我找不到用户名和密码字段。我检查了这些元素,并尝试通过id、xpath或css选择器查找它,但它给出了错误NosTouchElementException:Message:没有这样的元素:无法找到元素Python NoSuchElementException:消息:没有这样的元素:在尝试查找或访问元素标记时无法定位元素,python,selenium,selenium-webdriver,webdriver,webdriverwait,Python,Selenium,Selenium Webdriver,Webdriver,Webdriverwait,我找不到用户名和密码字段。我检查了这些元素,并尝试通过id、xpath或css选择器查找它,但它给出了错误NosTouchElementException:Message:没有这样的元素:无法找到元素 from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
if __name__ == "__main__":
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
option.add_argument("--start-maximized")
if getattr(sys, 'frozen', False):
chromedriver_path = os.path.join(sys._MEIPASS, "chromedriver.exe")
driver = webdriver.Chrome(chromedriver_path, options=option)
else:
driver = webdriver.Chrome(options=option)
driver.get("https://www.wix.com/")
loginPage = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/header/nav/a[2]"))).click()
usernameField = driver.find_element_by_id("input_4")
passwordField = driver.find_element_by_xpath("input_5")
usernameField.send_keys("user")
passwordField.send_keys("pass")
time.sleep(5)
driver.quit()
我得到的错误是:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"input_4"}
(Session info: chrome=71.0.3578.98)
(Driver info: chromedriver=2.44.609538 (b655c5a60b0b544917107a59d4153d4bf78e1b90),platform=Windows NT 10.0.17134 x86_64)
如果要查找
电子邮件
和密码
字段,可以尝试
usernameField = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "email")))
passwordField = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "password")))
usernameField.send_keys("user")
passwordField.send_keys("pass")
无法定位字段的原因:
驱动程序。通过id(“输入\ 4”)查找\元素\我看不到具有
的元素id=“输入\ 4”
值可能是动态的@id
-driver.find\u element\u by\u xpath(“input\u 5”)
不是有效的xpath语法。您可能需要使用“input\u 5”
,但无论如何,我也看不到具有/*[@id=“input\u 5”]
的元素id=“input\u 4”
单击()
,文本为登录且JavaScript/AjaxCalls完成后,这些元素即变为可见、启用和可点击的元素。因此,您必须诱导WebDriverWait使所需元素可单击,并且您可以使用以下解决方案:
- 最小代码块:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument("--disable-extensions") driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("https://www.wix.com/") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.LINK_TEXT, "Sign In"))).click() WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("paul") driver.find_element_by_css_selector("input[name='password']").send_keys("paul")
- 浏览器快照:
我在那页上没有看到
input_4
,我只看到input_1
和input_2
。另外,在find_element_by_xpath(“input_5”)
中,它不是有效的xpath。对我来说,它显示为input_0
和input_1
谢谢!它起作用了。对我来说,它显示为“input_4”,这就是我使用它的原因。我使用的xpath“/*[@id=“input_5”]”我只是在发布时忘了将其更改为id。但我也想问,为什么我需要等待?原因是什么?@paul,因为这些输入字段在页面源代码中不存在,所以您需要等待它们出现在DOM中