-2

Trying to click on service by using xpath

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")));

driver.findElement(By.xpath("/html/body/header/section/div[2]/div/div/ul/li[2]/a")).click();

but element is not getting selected/clicked.please help

john
  • 29
  • 9
  • 1
    don't you have an easier way to get that element? based on one id from an element which is a parent? You don't receive `NoSuchElementException`? – Turbut Alin Dec 16 '16 at 09:21
  • 2
    @john, can you provide the proper page source, So it would be easy to find which element is not clickable. – NarendraR Dec 16 '16 at 09:22
  • 2
    Add `HTML` sample for mentioned element. Also add exception log if you got any – Andersson Dec 16 '16 at 09:22

2 Answers2

0

driver.findElement(By.xpath("//*[@id='menu1']"))

try to use this. else

driver.findElement(By.id("menu1"))
NarendraR
  • 7,577
  • 10
  • 44
  • 82
Ramesh Bala
  • 89
  • 1
  • 13
0

try as follows (if the element is not inside a frame):

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@id='menu1']/span[text()='Service']")));

driver.findElement(By.xpath("//a[@id='menu1']/span[text()='Service']")).click();

If above code does not work. then it is most probably the element is inside a frame.

If the element is inside a frame (the element is a child of an iframe tag), then first switch to the element and find the element.

My detailed answer about switching b/w frames here, in selenium web driver how to choose the correct iframe

Community
  • 1
  • 1
Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
  • Never prefer to use absolute XPath (starting from `html` element) which has higher percentage of failures. Always prefer relative XPaths (use element attributes, id etc. if not available use nearest parent who is having id/ attributes etc and then find the child). – Naveen Kumar R B Dec 16 '16 at 09:53