Search code examples
seleniumxpathselenium-webdrivercss-selectorswebdriver

CSS Selector-How to locate Parent Element


Is there any way to locate parent element in CSS Selector? i am using below code but i am not getting the parent element.

WebElement we=dr.findElement(By.cssSelector("div[id='gf-BIG']:parent"));

I know there is a way in XPath but please let me know that how we can locate parent in CSS Selector.


Solution

  • Referring to the Is there a CSS parent selector? topic, there is no parent selector in CSS. Leave the "getting the parent" part to the XPath:

    WebElement we = dr.findElement(By.cssSelector("div[id='gf-BIG']"));
    WebElement parent = we.findElement(By.xpath(".."));