找不到元素异常,Selenium无法定位元素。

17

我希望能够在Selenium中找到我的TextField,但是我不知道该如何操作(我第一次使用Selenium)。

我尝试过:

 driver.findElement(By.id("originTextField"))
或通过Chrome开发工具生成的xPath和cssSelector字符串。
请帮帮我,我会感激您的解释。
这是HTML: enter image description here
3个回答

41

NoSuchElementException

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

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

  • When using WebDriver.findElement(By by) :

    //example : WebElement my_element = driver.findElement(By.xpath("//my_xpath"));
    
  • When using 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>标记内。
  • WebDriver实例正在查找WebElement,即使该元素尚未出现/可见于HTML DOM中。

解决方案

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

  • Adopt a Locator Strategy which uniquely identifies the desired WebElement. You can take help of the Developer Tools (Ctrl+Shift+I or F12) and use Element Inspector.

    Here you will find a detailed discussion on how to inspect element in selenium3.6 as firebug is not an option any more for FF 56?

  • Use executeScript() method to scroll the element in to view as follows :

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

    Here you will find a detailed discussion on Scrolling to top of the page in Python using Selenium

  • Incase element is having the attribute style="display: none;", remove the attribute through executeScript() method as follows :

    WebElement element = driver.findElement(By.xpath("element_xpath"));
    ((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('style')", element)
    element.sendKeys("text_to_send");
    
  • To check if the element is within an <iframe> traverse up the HTML to locate the respective <iframe> tag and switchTo() the desired iframe through either of the following methods :

    driver.switchTo().frame("frame_name");
    driver.switchTo().frame("frame_id");
    driver.switchTo().frame(1); // 1 represents frame index
    

    Here you can find a detailed discussion on Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?.

  • If the element is not present/visible in the HTML DOM immediately, induce WebDriverWait with ExpectedConditions set to proper method as follows :

    • To wait for presenceOfElementLocated :

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

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

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

参考资料

您可以在以下链接中找到基于客户端的Selenium相关讨论:


1
谢谢您的回答,能否请您提供executeScript()方法在Python中的等效方法?非常感谢! - Solal

3

你的代码是正确的,我怀疑页面没有完全加载时找到元素可能是导致问题的原因。

在查找元素之前尝试添加一个较长的等待时间,如果添加等待时间有效,则将其更改为“等待”。

这里是代码,它的意思是如果元素不存在,则等待10秒:

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

或者您可以输入 sleep(1000); 然后输入 driver.findElement(By.id("originTextField")) - RochaaP

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接