【问题标题】:NoSuchElementException when using Selenium使用 Selenium 时出现 NoSuchElementException
【发布时间】:2022-01-17 10:11:29
【问题描述】:

打开网页后webdriver找不到“name”元素?

Traceback (most recent call last):
  File "main.py", line 19, in <module>
    driver.find_element(By.NAME,"nickname").send_keys(username+Keys.ENTER)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 1244, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="nickname"]"}
  (Session info: chrome=)
Stacktrace:
#0 0x5574d3a05919 <unknown>

这是我使用的 HTML 源代码:

<input name="nickname" type="text" placeholder="Nickname" maxlength="15" id="nickname" data-functional-selector="username-input" class="sc-gTgzIj eFnEAY" autocomplete="off" value="" aria-expanded="false">

【问题讨论】:

  • 我们可以看看你的代码吗?

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

Aan &lt;inpt&gt; 元素是一个可点击 元素,理想情况下可以定位任何可点击 您需要为element_to_be_clickable() 诱导WebDriverWait 元素,您可以使用以下Locator Strategies 之一:

  • 使用CSS_SELECTOR

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#nickname[name='nickname'][placeholder='Nickname'][data-functional-selector='username-input']")))
    
  • 使用XPATH

    element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='nickname' and @name='nickname'][@placeholder='Nickname' and @data-functional-selector='username-input']")))
    
  • 注意:您必须添加以下导入:

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

参考文献

您可以在NoSuchElementException 中找到一些相关讨论:

【讨论】:

    【解决方案2】:

    如果我们在HTML DOM 中有唯一条目,请检查dev tools(谷歌浏览器)。

    检查步骤:

    Press F12 in Chrome -> 转到element 部分 -> 做一个CTRL + F -> 然后粘贴xpath 看看,如果你想要的element 用@ 得到高亮 987654328@匹配节点。

    如果这是唯一的//input[@id='nickname' and @name='nickname'],那么您还需要检查以下条件。

    1. 检查它是否在任何iframe/frame/frameset 中。
    2. 检查它是否在任何shadow-root 中。
    3. 确保在与元素交互之前正确呈现元素。输入一些hardcoded delayExplicit wait 再试一次。
    4. 如果您已重定向到 new tab/ or new windows 并且您没有切换到特定的 new tab/new window,否则您可能会遇到 NoSuchElement 异常。
    5. 如果您已切换到 iframe 并且所需的新元素不在同一个 iframe 上下文中,则首先 switch to default content 然后与之交互。

    切换到框架:

    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "frame XPath here")))
    

    切换到默认内容:

    driver.switch_to.default_content() 
    

    如果您使用显式等待,那么您还需要以下导入。

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

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 1970-01-01
      • 2021-08-16
      • 2022-07-06
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多