0
votes

any help is appreciated.

I am wondering if the firebug copy xpath always working for webdriver.

I have a page with a list, I could get the full xpath to find the first two elements, but could not do the same for 3, 4th.

For 3rd one it's not working:

WebElement element3=driver.findElement(By.xpath("/html/body/div[6]/div[2]/div/div[3]/div/div[3]/div/div[4]/div/div[4]/div/div/div/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/table/tbody[2]/tr[3]/td[5]/div"));

Error:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/html/body/div[6]/div[2]/div/div[3]/div/div[3]/div/div[4]/div/div[4]/div/div/div/div[1]/div[2]/div[1]/div/div[2]/div/div[2]/div[1]/div/div[2]/div[1]/table/tbody[2]/tr[3]/td[5]/div"}

My HTML:

<tr class="GKWRNW-BEX GKWRNW-BLYD GKWRNW-BELB GKWRNW-BON"> 
<td class="GKWRNW-BKW GKWRNW-BIYD x-grid-cell-first explorerGridRow" style="" cellindex="0"> 
<td class="GKWRNW-BKW GKWRNW-BIYD undefined x-treegrid-column explorerGridRow" style="" cellindex="1"> 
<div class="GKWRNW-BMW GKWRNW-BJYD" style=""> 
<div id="x-auto-390" class="GKWRNW-BPLC"> 
<div class="GKWRNW-BMLC"> 
<img style="height: 18px; width: 0px;" src="data:image/gif;=="> 
<img class="GKWRNW-BOLC" style="width: 16px" src="data:image/gif;base64,R0l=="> <span class="GKWRNW-BILC"></span>
1
Here is its html snippet: <tr class="GKWRNW-BEX GKWRNW-BLYD GKWRNW-BELB GKWRNW-BON"> <td class="GKWRNW-BKW GKWRNW-BIYD x-grid-cell-first explorerGridRow" style="" cellindex="0"> <td class="GKWRNW-BKW GKWRNW-BIYD undefined x-treegrid-column explorerGridRow" style="" cellindex="1"> <div class="GKWRNW-BMW GKWRNW-BJYD" style=""> <div id="x-auto-390" class="GKWRNW-BPLC"> <div class="GKWRNW-BMLC"> <img style="height: 18px; width: 0px;" src="data:image/gif;=="> <img class="GKWRNW-BOLC" style="width: 16px" src="data:image/gif;base64,R0l=="> <span class="GKWRNW-BILC"></span> - newbie1
You should put your code in your question, not in comments - Jan
did you modify your DOM element after the code has been written? If any DOM element is added or removed then most likely the x-path will change too. - Razib

1 Answers

0
votes

Using x-path to find an element is not an efficient way. Because any changes in DOM element (eg. - adding or removing of a new DOM element like a div) will results in a change in x-path.

So better to use id to each DOM element and use By.id() -

WebElement element = driver.findElement(By.id("someId::button"));