0

I swear I've searched google and SE for hours and I just couldn't find anything that applies to my case.

I've located my link using:

WebElement element = driver.findElement(By.linkText("News"));

This, however, does not return the link (<a> tag) but returns the a <span> with the linkText.

Here is how the link structure looks like:

<a href="________" itemprop="url" style="height: 88px; line-height: 88px;">
    <span class="avia-bullet"></span>
    <span class="avia-menu-text">News</span>
    <span class="avia-menu-fx">
        <span class="avia-arrow-wrap">
            <span class="avia-arrow"></span>
        </span>
    </span>
    <span class="dropdown_available"></span>
</a>

So, when I do: element.click();, the whole browser goes down with "ElementNotInteractableException". I thought this may be time-out, but I've given it a lot of time and it still fails.
I suspect I just can't click a <span>.

But how do I refer to his parent (the <a>)?

I tried:

element=element.findElement(By.xpath("parent"));
element=element.findElement(By.xpath(".."));
DraxDomax
  • 1,008
  • 1
  • 9
  • 28
  • try element=element.findElement(By.xpath("./..")); './' in xpath represent current node. you need to use ./ to tell selenium find parent start from current node: News – yong Oct 20 '17 at 04:24

3 Answers3

0

With complex HTML, Selenium's default methods won't work as expected.

How about trying with different methods of xpath functions:

For instance, try the one below:

driver.findElement(By.xpath("//a[contains(.,'News')]")).click();

This SO post has more details on other path functions: XPath contains(text(),'some string') doesn't work when used with node with more than one Text subnode

parishodak
  • 4,506
  • 4
  • 34
  • 48
0

You cant make any actions over span tag. So better to get any of div/link tag so that you can perform action over it.

//span[.='News']/ancestor::a
//span[contains(.,'s')]/ancestor::a

<!-- Any of its innerHTML Text (with out case-sensitive)-->
//a[contains(.,'YY')]

<!-- Next followed child nodes. -->
//a[span[text()='News']]
//a[span='News' and @style="height: 88px; line-height: 88px;"]

<!-- Any of its child - child nodes. -->
//a[.//span[text()='SS']]


//a[[.='News'] and [.//span[contains(.,'YY')]]]
//a[span='News' and [.//span[contains(.,'YYY')]] ]

Test XML:

<a href="________" itemprop="url" style="height: 8px; line-height: 88px;">
    <span class="avia-menu-text">News</span>
    <span class="avia-menu-fx">
        <span class="avia-arrow-wrap">SS
            <span class="avia-arrow">YYY</span>
        </span>
    </span>
</a>
Yash
  • 9,250
  • 2
  • 69
  • 74
0

Write xpath as you describe it:

you need:

a link that is using a span defined within it?

so here is xpath:

//a[.//span]

and make span more specific:

//a[.//span[text()='News']]

or

//a[.//span[contains(@class,'avia-menu-text')][text()='News']]

You can also add some specification to a element, or it's parent item to be sure that you found correct item.

Vitaliy Moskalyuk
  • 2,463
  • 13
  • 15