9

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.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
J_Coder
  • 707
  • 5
  • 14
  • 32

3 Answers3

20

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(".."));
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

If it would help here is example how get it by xpath

WebElement parentElement = driver.findElement(By.xpath("//div[@id='gf-BIG']/parent::parentElementTag"));

parentElementTag - could be div/span/tr - depends on your case

1

If the parent is say div, you could try something like below

div div[id='gf-BIG']
Indu Devanath
  • 2,068
  • 1
  • 16
  • 17