selenium - NoSuchElementException,Selenium 无法定位元素

标签 selenium selenium-webdriver css-selectors webdriver selenium-chromedriver

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

我尝试过:

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

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

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

这是 html:

enter image description here

最佳答案

NoSuchElementException

org.openqa.selenium.NoSuchElementException通常称为 NoSuchElementException 扩展 org.openqa.selenium.NotFoundException这是 WebDriverException 的一种类型.

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

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

解决方案

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

  • 采用Locator Strategy它唯一标识所需的WebElement。您可以借助开发人员工具(Ctrl+Shift+IF12)并使用元素检查器

    在这里您可以找到关于 how to inspect element in selenium3.6 as firebug is not an option any more for FF 56? 的详细讨论

  • 使用 executeScript() 滚动元素查看的方法如下:

    WebElement elem = driver.findElement(By.xpath("element_xpath"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();", elem);
    

    在这里您可以找到关于 Scrolling to top of the page in Python using Selenium 的详细讨论

  • 如果元素具有属性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
    

    在这里您可以找到关于 Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java? 的详细讨论.

  • 如果该元素未立即在 HTML DOM存在/可见,则引发 WebDriverWaitExpectedConditions设置为正确的方法如下:

    • 等待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']")));
      
<小时/>

引用

您可以找到Selenium基于客户的相关讨论:

关于selenium - NoSuchElementException,Selenium 无法定位元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61898694/

相关文章:

css - selenium:如何检查某些文本是否为粗体

python - 使用 Selenium 抓取 Understat 图表数据的问题

selenium - 如何使用 selenium webdriver 在客户端启动浏览器

java - 为什么我无法返回selenium方法中的值?

html - 带元素前缀的高级元素选择器在 Chrome 中不起作用

java - 如何创建 isSecure 标志为 false 的 org.openqa.selenium.Cookie

java - Selenium 无法接受 google chrome 的警报 [java]

java - 如何验证 selenium 2 (Selenium WebDriver) 中存在或可见的元素

css - 什么是 :horizontal and :vertical pseudo selectors for?

css - .something :first-child as a selector? 的性能