user2166417
user2166417

Reputation: 51

Selenium PHP - findElement by xpath, complex xpath not working

Dears,

I'm facing a problem with the following xpath which returns no element if $pos is greater than 1.

note that there is more than 1 div[@class="panel-heading"] in form[contains(@action, "/myaction?")]

$this->webdriver->findElement(WebDriverBy::xpath('//form[contains(@action, "/myaction?")]//div[@class="panel-heading"][' . $pos . ']//a[contains(@onclick, "do_something")]'))->click();

Any idea what's wrong with my xpath ?

Thanks in advance, Lionel

Upvotes: 1

Views: 643

Answers (2)

Saurabh Gaur
Saurabh Gaur

Reputation: 23845

You should try using () if you are using index in your xpath as below :-

$this->webdriver->findElement(WebDriverBy::xpath('(//form[contains(@action, "/myaction?")]//div[@class="panel-heading"])[' . $pos . ']//a[contains(@onclick, "do_something")]'))->click();

Upvotes: 0

lauda
lauda

Reputation: 4173

Check if you have only one single link with that action, search only for
//a[contains(@onclick, "do_something")] and see if you identify the do_something that make is unique.
Avoid using [number] because it can break easy if any of the abode div's is gone.

If you get issues when $pos is greater than one means that the div which gets this condition is not at the same level with any other div.

Upvotes: 0

Related Questions