1
<ul class="drillDownMenu l_drillDown" style="left: -498px;">
<li class="hasSubs">
<a id="RAL10" href="javascript:;">
<ul class="active">
<li class="hasSubs">
<li class="hasSubs">
<a id="AL101117" href="javascript:;">AR Invoices</a>
<ul class="displayed active">
<li>
<a id="FAL10111726" onclick="LoadQueryWindow(this,'104')">AR Invoices</a>
</li>
<li>
<a id="FAL10111727" onclick="LoadQueryWindow(this,'134')">All AR Invoices 1</a>
</li>
</ul>
</li>

I am trying to use the below driver statement,

driver.findElement(By.xpath("//*[contains(@id,'FAL10111727')]")).click();

But this selects the first element, I want to select the last element in the list.

Thanks

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

3 Answers3

0

Try this xpath expression:

(//ul[ @class='drillDownMenu l_drillDown']//li)[last()]

it picks all li elements that are childs of the topmost ul, then picks the last element from the whole set.

krokodilko
  • 35,300
  • 7
  • 55
  • 79
0

As per the HTML you have shared,

To select the last 'li' which is changing dynamically for each run, All AR Invoices 1 in this case you can use :

driver.findElement(By.xpath("//ul[@class='displayed active']//following::li[last()]")).click();

But, I suppose the <li> tags won't receive a click and you have to invoke click() method on the inner <a> tag instead as follows :

driver.findElement(By.xpath("//ul[@class='displayed active']//following::li[last()]/a")).click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I tried using this, Still, it shows element not found, driver.findElement(By.xpath("//*[contains(@id,'mCSB_1_container')])::[last()] can i use something like this, Using the ID of the group element and selecting the last item from that list. – balu mahesh Nov 23 '17 at 16:32
  • I really like the way you wrote it, I am trying my best to check this in all way let me see what i can do – balu mahesh Nov 23 '17 at 16:37
  • `element not found` just induce `WebDriverWait` you will be through. For a quick check add `Thread.sleep(10000)` for debugging just before the line. – undetected Selenium Nov 23 '17 at 16:39
  • I had given the thread. sleep(5000) This is the Error which I am seeing, org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 36 milliseconds – balu mahesh Nov 23 '17 at 18:42
  • @balumahesh Great !!! That means element is found now but not visible. That solves your Question. Please Accept the Answer by clicking on the tick mark beside my Answer which is just below the Vote Down arrow so that the tick mark turns green. – undetected Selenium Nov 23 '17 at 19:08
  • @balumahesh To address `ElementNotVisible` see this QA https://stackoverflow.com/questions/47108512/elementnotvisibleexception-selenium-python – undetected Selenium Nov 23 '17 at 19:09
0

//I tried using this which worked

driver.findElement(By.cssSelector("ul.displayed li:last-child a")).click();