NoSuchElementException-无法找到元素 [英] NoSuchElementException - Unable to locate element

查看:100
本文介绍了NoSuchElementException-无法找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入框,例如我在这里使用的输入框,其HTML是

I have an input box like the one I am using here to enter my question, and whose HTML is

<body id="tinymce" class="mce-content-body" contenteditable="true" onload="window.parent.tinymce.get('Description').fire('load');" spellcheck="false" style="padding-bottom: 50px; padding-left: 1px; padding-right: 1px; overflow-y: hidden;">
<p>
<br data-mce-bogus="1"/>
</p>
</body>

每次,我都尝试输入一些文字

Every-time, I try to enter some text to it

@FindBy(xpath="//body[@id='tinymce']") WebElement Category_Body;
Category_Body.sendKeys("Android Smart Phone - 16GB");

我得到了错误-

org.openqa.selenium.NoSuchElementException:无法找到元素:{"method":"xpath","selector":"//body [@ id ='tinymce']"}

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//body[@id='tinymce']"}

推荐答案

如果将NoSuchElementException作为提供的异常,则可能有以下原因:-

If you are getting NoSuchElementException as your provided exception, There may be following reasons :-

  • 可能是当您要查找元素时,它不会出现在DOM上,因此您应实现WebDriverWait,直到元素可见,如下所示:-

  • May be when you are going to find element, it would not be present on the DOM, So you should implement WebDriverWait to wait until element visible as below :-

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tinymce")));
 Category_Body.sendKeys("Android Smart Phone - 16GB");

  • 可能是此元素在任何frameiframe内部.如果是的话,则需要在找到以下元素之前切换frameiframe:-

  • May be this element is inside any frame or iframe. If it is, you need to switch that frame or iframe before finding the element as below :-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name"));
    
    //Now find the element 
    WebElement Category_Body = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("tinymce")));
     Category_Body.sendKeys("Android Smart Phone - 16GB");
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    

  • 这篇关于NoSuchElementException-无法找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆