简体   繁体   中英

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.

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:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM