0

EXception occur (element not found @ given xpath ) when try find element by x path in eclipse project using ie driver in maven project , this element is inside iframe, when inspect desired element, I found it in the following structure:

 <div id="idName">
   <div class="container">
     <div>
       <p class="v1">..............notice this repeated for many different href
         <a href= 
          <span class="v2".........notice this repeated for many different href
           <b>part of label</b>
               rest of label
               <br></br>

When I used

webelement.findElement(By.xpath("//*[@id='idName']/div/div/p/a")).getAttribute("href"))

this results in exception when run No Such Element Exception , seems I miss something in the relative XPath, any ideas ?

Note: i tried to switch driver to iframe before find element but it didn't help and still having exception .

Thanks A lot for any help .

Mai Saad
  • 249
  • 1
  • 3
  • 10
  • Check whether your `
    ` located inside `iframe`/`frame`
    – Andersson Nov 26 '16 at 10:55
  • 1
    @MaiSaad `href` is attribute of `a`, not `p`, so, at least you should try the following XPath : `//*[@id='idName']/div/div/p/a` – har07 Nov 26 '16 at 10:59
  • @har07: I tried this but gives same exception ! – Mai Saad Nov 26 '16 at 11:42
  • @Andersson: yes I checked and it is located inside iframe , how will this help ? – Mai Saad Nov 26 '16 at 11:43
  • @Andersson : even after adding switching to frame using – Mai Saad Nov 26 '16 at 12:11
  • @Andersson : even after adding switching to frame using (driver.switchTo().frame(driver.findElement(By.tagName("iframe")))).findElement(By.xpath("//*[@id='idName']/div/div/p/a")).getAttribute("href"); still giving me same exception for the given xpath !! – Mai Saad Nov 26 '16 at 12:18
  • I`m using internet explorer if this may help! , any ideas?? – Mai Saad Nov 26 '16 at 12:40
  • Are you sure that there is only one `iframe`? you should specify exact `iframe` you want to switch to – Andersson Nov 26 '16 at 12:46
  • yes only one iframe and it has id if this id can help! – Mai Saad Nov 26 '16 at 12:56
  • I tried to wait after switch driver till presence of element located by xpath but still having exception that expected condition of presence of element failed :( – Mai Saad Nov 26 '16 at 12:59
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [ask] page for help clarifying this question. – JeffC Nov 26 '16 at 16:32
  • refer switching b/w frames example here http://stackoverflow.com/a/40759300/2575259 (in python, but concept is same) – Naveen Kumar R B Nov 27 '16 at 10:58
  • finally works : switch to iframe using driver.switchto().frame("frameid").findElement(By.xpath("//*[@id='idname']/div/div/p/a")).getAttribute("href") this returns the right attribute and gives no exception , thanks. – Mai Saad Nov 30 '16 at 11:32

2 Answers2

0

Based on the HTML provided above best bet would be to go for first anchor tag following the div 'ID' assuming that ID is unique throughout the page you are working on.

Modify the locator as below and give it a try:

//*[@id='idName']//following::a

If this returns more than 1 link in your page then you can add an index at the end as below:

(//*[@id='idName']//following::a)[1]

SelThroughJava
  • 321
  • 3
  • 14
  • unfortunately i still having exception org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="idname"]/]/div/div/p::a"} – Mai Saad Nov 29 '16 at 18:57
0

when the element we want to get it`s attribute exist inside iframe then we shall switch to this frame first : driver.switchTo().frame("frameid").findElement(By.xpath("element xpath")).getAttribute("href"); then switch to default content to go out of frame: driver.switchTo().defaultContent();

Mai Saad
  • 249
  • 1
  • 3
  • 10