Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

selenium webdriver - NoSuchElementException - Unable to locate element

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

I get the error -

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

  • 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");
    
  • 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();
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...