NoSuchElementException,Selenium 无法定位元素

问题描述 投票:0回答:4

我想在 selenium 中找到我的 TextField,但我不知道如何(我第一次使用 Selenium)。

我尝试过:

 driver.findElement(By.id("originTextField"))

或者通过开发工具中chrome生成的xPath和cssSelector字符串。

请帮助我,我将不胜感激。

这是 html:

selenium selenium-webdriver css-selectors webdriver selenium-chromedriver
4个回答
42
投票

NoSuchElementException

org.openqa.selenium.NoSuchElementException,俗称 NoSuchElementException 扩展了 org.openqa.selenium.NotFoundException,它是 WebDriverException 的一种类型。

NoSuchElementException 可以在以下 2 种情况下抛出:

  • 使用时

    WebDriver.findElement(By by)
    :

    //example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
    
  • 使用时

    WebElement.findElement(By by)

    //example : WebElement my_element = element.findElement(By.xpath("//my_xpath"));
    

根据 JavaDocs,就像任何其他 WebDriverException 一样,NoSuchElementException 应包含以下 常量字段

Constant Field      Type                                        Value
SESSION_ID          public static final java.lang.String        "Session ID"
e.g. (Session info: chrome=63.0.3239.108)

DRIVER_INFO         public static final java.lang.String        "Driver info"
e.g. (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86)

BASE_SUPPORT_URL    protected static final java.lang.String     "http://seleniumhq.org/exceptions/"
e.g. (For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html)

原因

NoSuchElementException的原因可能是以下任一原因:

  • 您采用的定位器策略无法识别HTML DOM中的任何元素。
  • 您采用的定位器策略无法识别该元素,因为它不在浏览器的视口内。
  • 您采用的定位器策略标识了该元素,但由于属性style =“display:none;”的存在而不可见。
  • 您采用的
  • 定位器策略不能唯一地标识HTML DOM中所需的元素,并且当前找到一些其他隐藏/不可见元素。
  • 您尝试查找的
  • WebElement 位于 <iframe>
     标签内。
  • 即使在元素在
  • HTML DOM 中出现/可见之前,WebDriver 实例也会寻找 WebElement

解决方案

解决

NoSuchElementException 的解决方案可以是以下任意一种:

  • 采用

    定位器策略,它唯一标识所需的WebElement。您可以借助开发人员工具Ctrl+Shift+IF12)并使用元素检查器

    在这里您将找到关于

    如何检查 selenium3.6 中的元素的详细讨论,因为 Firebug 不再是 FF 56 的选项?

  • 使用

    executeScript()

     方法滚动元素查看如下:
    
    

    WebElement elem = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
    
    
    在这里您将找到关于

    使用 Selenium 在 Python 中滚动到页面顶部

    的详细讨论
  • 如果元素具有属性

    style="display: none;",请通过 executeScript()

     方法删除该属性,如下所示:

    WebElement element = driver.findElement(By.xpath("element_xpath")); ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element) element.sendKeys("text_to_send");
    
    
  • 要检查元素是否位于

    <iframe>

     内,请通过以下任一方法向上遍历 
    HTML 以找到相应的 <iframe>
     标签和 
    switchTo()
     所需的 
    iframe :

    driver.switchTo().frame("frame_name"); driver.switchTo().frame("frame_id"); driver.switchTo().frame(1); // 1 represents frame index
    
    
    在这里您可以找到关于

    Selenium Webdriver Java 中不使用 driver.switchTo().frame(“frameName”) 是否可以切换到框架中的元素?.

    的详细讨论
  • 如果该元素不是立即在

    HTML DOMpresent/visible,则将 ExpectedConditions 设置为正确的方法来引发 WebDriverWait,如下所示:

    • 等待

      presenceOfElementLocated :

      new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      
      
    • 等待

      visibilityOfElementLocated

      new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      
      
    • 等待

      elementToBeClickable

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='buttonStyle']//input[@id='originTextField']")));
      
      

参考

您可以在以下位置找到

Seleniumpython

  • 使用 Chrome 时的 Selenium“selenium.common.exceptions.NoSuchElementException”

3
投票
您的代码是正确的,我怀疑该问题导致您找到该元素时页面未完全加载。

尝试在查找元素之前添加长时间睡眠,如果添加睡眠有效,请将睡眠更改为等待。

这是代码,这意味着如果元素不存在则等待10秒:

element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "originTextField")) )
    

0
投票
driver.implicitly_wait(30) 帮助我解决了这个问题

谢谢你的建议!!


-2
投票
下面的代码将帮助您解决问题

wait = new WebDriverWait(driver, 60); wait.until(ExpectedConditions.presenceOfElementLocated(By.id("originTextField")));

wait = new WebDriverWait(driver, 90); wait.until(ExpectedConditions.visibilityOf(element)).wait(20);
    
© www.soinside.com 2019 - 2024. All rights reserved.