NoSuchElementException:消息:没有这样的元素:在尝试查找或访问元素标签时无法找到元素

时间:2019-01-21 20:25:08

标签: python selenium selenium-webdriver webdriver webdriverwait

我找不到用户名密码字段。我检查了元素,并尝试通过id,xpath或css选择器找到它,但是它给我错误NoSuchElementException:消息:没有这样的元素:无法找到元素。

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)

2 个答案:

答案 0 :(得分:1)

如果您要查找emailpassword字段,可以尝试

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

找不到字段的原因:

  • driver.find_element_by_id("input_4")-我看不到id="input_4"的元素。 @id的值可能是动态的。
  • driver.find_element_by_xpath("input_5")-"input_5"无效的XPath语法。您可能需要使用//*[@id="input_5"],但无论如何,我也看不到id="input_4" ...的元素...

答案 1 :(得分:0)

用户名密码字段是Angular元素,并且可见启用 clickable ,一旦您在元素上调用文本为登录 JavaScript / AjaxCalls 的元素上的click()完成。因此,您必须诱使 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")
    
  • 浏览器快照:

wix_login

  

注意:您可以在Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

中找到相关的详细讨论