RShome
RShome

Reputation: 537

How to reduce the Xpath to make it shorter?

I am trying to get the value of a menu item in an Angular wesbite. If I put the detailed Xpath, I can select the menu item from the drop down.

How would I get the XPath significantly shorter? Apologies if this is a terrbile question, I am pulling my hair out!

I have used the exact XPath but my code got rejected when I checked in.

driver.FindElement(By.XPath("/html/body/modal-container/div/div/inv-education-modal/article/div[2]/div/div[1]/div[1]/div/inv-drop-down-select/section/div/ul/li[3]/a/span"));

I actually want to pass a parameter so it selects the relevant dropdown value, but it's an Angular site so I can't use a SelectElement.

Upvotes: 0

Views: 2026

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

There is an <a> tag before your <span> tag which indicates a hyperlink.

Normally hyperlinks have href attribute and the associated text like:

<a href="http://example.com>Click me!</a>

which will look in your site as:

Click me!

a "shorter" XPath which will look for a hyperlink with "Click me" text will look as:

//a[@href='Click me!']

and if you want the next span - //a[@href='Click me!']/span

References:

Upvotes: 1

Related Questions