Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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']"}

like image 879
Bimlesh Avatar asked Aug 28 '16 11:08

Bimlesh


People also ask

How do you solve unable to locate an element?

We may encounter the error - unable to locate element while working with Selenium webdriver. This leads to NoSuchElementException. This type of exception is thrown when there is no element on the page which matches with the locator value. Check if there is any syntax error in our xpath expression.

How would you handle unable to locate an element in Selenium?

you must check is this element in and IFRAME if yes then first switch into iframe and secondly if by ID is not working then Use Xpath or CSS path can you please share HTML source with me.

What is NoSuchElementException in Python?

When we try to run the code below, it raises NoSuchElementException . This is because we try to find an element called userNam , but the webpage element name in the source code is userName .

Will a find element type call throws NoSuchElementException when it can't find the element?

The findElement method throws a NoSuchElementException exception when the element is not available on the page. Whereas, the findElements method returns an empty list when the element is not available or doesn't exist on the page. It doesn't throw NoSuchElementException.


1 Answers

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();
    
like image 144
Saurabh Gaur Avatar answered Oct 05 '22 10:10

Saurabh Gaur