Sunday 14 April 2013

Finding Parent Element in Selenium/WebDriver

Hello All,

This post tells about finding parent element of any element using Selenium/Web Driver. Many times we need to perform some action on elements based on their parents. This post will give you some idea to find out the parent element by using xpath.

Code:

//##################################################//
public WebElement GetParentElement(WebElement elem)
{
    WebElement parentElem=null;
    if(elem !=null)
    {
        parentElem=elem.findElement(By.xpath(".."));
    }
    return parentElem;          
}
//##################################################//


Thanks
Dibya

2 comments:

  1. I can see that you are are genuinely passionate about this! great information.
    thank you...!


    Big data online training

    ReplyDelete
  2. I'm using Selenium/Webdriver for Appium with the Java-client library for an Android App.

    I'm trying to get the "LinearLayout" (clickable) parent of a TextView (non-clickable). I have no problem with the WebElement "TextView" but when i try to get his parent with this method I get this error:

    [debug] Responding to client with error: {"status":13,"value":{"message":"An unknown server-side error occurred while processing the command. (Original error: Cannot use xpath locator strategy from an element. It can only be used from the root element)","origValue":"Cannot use xpath locator strategy from an element. It can only be used from the root element"},"sessionId":"f7184c55-8ba2-41b4-8bbf-144af1763a7d"}

    ReplyDelete

Related Posts

Related Posts...