When working with Selenium Basic, I attempted to click on the element 'MyLocalLang' nested within <li>
tags.
Here is the source:
<div id="navBar" class="NavBar">
<nav id="navMenu" class="Wrapper"> == 40
<ul class="navMenuUL">
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li> ... </li>
<li>
<a class="nav" href="//en.mysite.com/item/">MyLocalLang</a>
</li>
<li> ... </li>
</ul>
</nav>
I need to specifically select the <li>
tag that contains the desired link, as the other elements are irrelevant.
The code
driver.FindElementByXPath("//*[@id='navMenu']/ul/li[9]/a")
works, but:
- The target link may not always be in the ninth position;
- The link text could be in various languages, such as MaLangueLocale, MiaLinguaLocale, MiIdiomaLocal, etc.;
- The URL could also vary in different languages, like fr.mysite.com, it.mysite.com, en.mysite.com, etc.;
- The @class 'nav' attribute is present in all
<li>
tags and is therefore not useful for targeting.
Despite several attempts, I have been unable to accomplish this task. I have tried:
driver.FindElementByXPath("//*[@id='navMenu']/ul/li[contains(@href,'*mysite.com/item/')]/a").Click
with and without the final /a
, among other variations.
If anyone can assist me in constructing the correct xpath expression to click on the link regardless of its language or position within the list, please advise. Please note that I am utilizing Selenium in a VBA project.
Thank you in advance.