简体   繁体   中英

What is the XPath for the following element?

I need to find the XPath for the following element below:

<a> href="/resident/register/">Register another device </a>

I assumed the solution would be

$x("//*[contains(@href, 'resident/register')]")

But this has returned nothing. Any ideas?

Your HTML is malformed.

Change

<a> href="/resident/register/">Register another device </a>

to

<a href="/resident/register/">Register another device </a>

then your XPath will work as expected.

If your HTML is fixed, then you'll have to adjust your XPath to test the element content rather than the href attribute content:

//a[contains(.,'resident/register')]

but, although this can select the malformed a element, it won't be clickable since it lacks a proper href attribute.

To start with @kjhughes nailed one of the issue with the HTML ie HTML is malformed , as you must have tried to provide a correct HTML from your understanding.

The actual HTML I suppose is as follows:

<a href="/resident/register/">Register another device </a>

So an effective XPath for the element can be either of the following:

  • XPath:1

     "//a[contains(@href, '/resident/register') and contains(.,'Register another device')]"
  • XPath:2

     "//a[contains(@href, '/resident/register') and normalize-space()='Register another device']"

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