Selenium chromedriver unable to locate href tag nested inside class tags

58 views
Skip to first unread message

asad ali

unread,
Sep 29, 2020, 8:02:35 AM9/29/20
to Selenium Users
Hi everyone,

I'm using `selenium` web-driver, I want to capture or click on 
this HTML tag as

`\<a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search"\>`

I tried using 
`Driver.findElement(By.xpath(//a[@href='#/trend-analysis/tag-search']")).click();`



    <div class="collapse navbar-collapse style-scope app-shell" id="navbarNav">
                    <ul class="app-nav navbar-nav mr-auto style-scope app-shell">
                        <li class="nav-item style-scope app-shell">
                            <a class="nav-link p-3 px-4 style-scope app-shell" href="/#/dashboard">Dashboard</a>
                        </li>
                        <li class="nav-item style-scope app-shell">
                            <a class="nav-link py-3 px-4 style-scope app-shell active" href="/#/trend-analysis/tag-search">Trend
                                Analysis</a>
                            <ul class="sub-nav d-flex flex-row flex-nowrap list-unstyled style-scope app-shell">
                                <li class="sub-nav-item style-scope app-shell">
                                    <a class="sub-nav-link style-scope app-shell active" href="#/trend-analysis/tag-search">
                                        Trend Analysis
                                    </a>


**error**

    Unable to locate element: {"method":"xpath","selector":"//*[@href='/#/trend-analysis/tag-search']"}

my guess is that it doesn't detect this href because its buried deep under many `<a>` and `<class>` tags. Please guide me.

joseph...@gmail.com

unread,
Sep 29, 2020, 1:29:50 PM9/29/20
to Selenium Users
Your HTML says the link has this href:
href="#/trend-analysis/tag-search"

Your code says you are using this xpath:
//a[@href='#/trend-analysis/tag-search']

However, the error message says the code is actually trying to find this xpath:
//*[@href='/#/trend-analysis/tag-search']

The error message indicates that the xpath being used is searching for //* instead of //a (shouldn't matter, but will be a slower search due to checking all elements instead of only <a> elements) and that the href attribute should start with /# where the HTML only starts with # (this is where I think the problem is).

You can test xpaths in the browser dev tools by going to the console, entering in $x("<your xpath here>") and hitting enter to see what comes back.  If it works in the browser console, you should be able to get it to work in your code.
Reply all
Reply to author
Forward
0 new messages