Selenium Element not corresponding to Click

midaste

New Member
Joined
Jan 16, 2025
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I have the following element that is the 4th element on a nav bar within a website:

1737049964147.png


<a href="#step3" data-toggle="tab" data-bind="click: function(){ viewModel.GetRequiredDocuments(viewModel.xxxx); viewModel.xxxx(); }" aria-expanded="true">Documents</a>

I am able to select the element. I have confirmed that I have the right element by locals window in vba and debug print the label (ie Documents). However, when I send the "click", the website does not move to the proper tab.

It is not a wait until it is clickable problem as I have tried an explicit wait in which period I am to click on the element using the mouse (as a test).

Here is my relevant code:

Set documentLink = driver.FindElementByXPath("/html/body/div[11]/div[1]/div/ul/li[4]")
documentLink.Click

Any help is appreciated. Thanks.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
A few things that come to mind:
1. Maybe it's .Click()
2. Maybe that li[4] element does not have a click event associated but a nested child does
3. Maybe that li[4] element does not have a "label", but a nested child does

Without the website in question, or its structure, it's going to be hard to tell why that does not work, but I've never had an issue targeting tags, ids or names.
 
Upvote 0
Reading again, I see you have an anchor tag with the word Documents, so, out of pure speculation, you could try with
/html/body/div[11]/div[1]/div/ul/li[4]/a
You could also post the inner html code of the unsorted list (ul tag) if you don't want to post the website link.
 
Upvote 0
A few things that come to mind:
1. Maybe it's .Click()
2. Maybe that li[4] element does not have a click event associated but a nested child does
3. Maybe that li[4] element does not have a "label", but a nested child does

Without the website in question, or its structure, it's going to be hard to tell why that does not work, but I've never had an issue targeting tags, ids or names.

It only has one nested element which is what I pasted above. I have tried it with and without the "/a" at the end of the path. Adding () to Click to make it Click() produces an error in VBA.

Unfortunately the website is one that you would have to have login access to use so I cannot provide it. I will provide a little more of the elements. I think it might have to do with the class="Active" vs class == $0 part which I just notice the active switches to what is clicked.

<div class="container" id="Rater">

<div class="navbar" data-bind="visible:viewModel.xxxx()">

<div class="navbar-inner">

<ul class="nav nav-tabs nav-justified">

<li class="Active">…</li>

<li>…</li>

<li> …</li>

<li class ==$0>

<a href="#step3" data-toggle="tab" data-bind="click: function(){ viewModel.GetRequiredDocuments(viewModel.xxxx); viewModel.xxxx(); }" aria-expanded="true">

Documents

</a>

</li>

</ul>

</div>

</div>
 
Upvote 0
Maybe
driver.ExecuteScript('viewModel.GetRequiredDocuments(viewModel.xxxx);')

Or
driver.FindElementsByTag("ul")(4).FindElementsByTag("a")(0).Click
 
Upvote 0
I think you need /a at the end of the xpath because that's the element with the embedded click function.

Try this:
VBA Code:
    Set documentLink = driver.FindElementByLinkText("Documents")
    documentLink.Click
    'or
    driver.ExecuteScript "arguments[0].click();", documentLink
 
Upvote 0
Maybe
driver.ExecuteScript('viewModel.GetRequiredDocuments(viewModel.xxxx);')

Or
driver.FindElementsByTag("ul")(4).FindElementsByTag("a")(0).Click

Tried both of these as-is and with several variations on the theme and didn't work. Thank you for trying.
 
Upvote 0
Tried both of these as-is and with several variations on the theme and didn't work. Thank you for trying.
What happened?

By the way, made the mistake of targeting an unsorted list tag, but it was a list item I should have been targeting:
driver.FindElementsByTag("li")(4).FindElementsByTag("a")(0).Click

Sometimes, just clicking doesn't work, you have to actually dispatch the event.
 
Upvote 0
I think you need /a at the end of the xpath because that's the element with the embedded click function.

Try this:
VBA Code:
    Set documentLink = driver.FindElementByLinkText("Documents")
    documentLink.Click
    'or
    driver.ExecuteScript "arguments[0].click();", documentLink

Thank you for your reply. I have already tried both of these as I found them through google search and tried them again for good measure and they do not work. Not sure why as they should and there in lies the issue. Thanks again.
 
Upvote 0
What happened?

By the way, made the mistake of targeting an unsorted list tag, but it was a list item I should have been targeting:
driver.FindElementsByTag("li")(4).FindElementsByTag("a")(0).Click

Sometimes, just clicking doesn't work, you have to actually dispatch the event.
The script ran without error, but also without causing the move to the nav tab desired. The versions of the find by element gave an error: index was outside the bounds of the array. Although I am pretty sure the issue is not that I do not have the correct element located. I can see it in the locals window assigned to the element variable. Thanks.
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,734
Members
453,369
Latest member
juliewar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top