-2

I've gone through the Selenium Documentation for locating elements, but I can't seem to figure out how to find the element in my code.

Here is my code from my .cshtml:

<a onclick="alter('@key', '@value')" href="#edit" id="@key-display">@value</a>

I am trying to locate and click the @value at the end. Here is what it looks like when I inspect the value on Chrome:

<a onclick="alter('February 9, 2018', '1.00000')" href="#edit" id="February 9, 2018-display">1.00000 gallons</a>

I am able to locate the element by link text like this:

chromeDriver.FindElementByLinkText("1.00000 gallons").Click();

However, the link text will change constantly and I want to be able to locate it after it changes.

I have tried locating by several ways:

chromeDriver.FindElementByLinkText("@value").Click();

chromeDriver.FindElementByXPath("//a[@id='@key-display']").Click();

chromeDriver.FindElementById("@key-display").Click()
  • Is this link contained in another element which has a static identifier, such as a div with an id? – Ryan Wilson Feb 20 '18 at 18:25
  • It is contained inside a with no static id – Idon'tknowhowmycodeworks Feb 20 '18 at 18:29
  • How do you know you want this particular link? Does it display some specific value of text? Is it contained in a specific row of your table? – Ryan Wilson Feb 20 '18 at 18:32
  • Did you try to match required link by constant text part with something like `chromeDriver.FindElementByPartialLinkText("gallons").Click();`? – Andersson Feb 20 '18 at 18:33
  • Without knowing what the other TD elements contain, I can only guess that the following would be unique: `chromerDriver.FindElementByXpath("//a[ends-with(@id,'-display'];` but again, if other elements use the same pattern, no good. – Bill Hileman Feb 20 '18 at 18:51
  • Do you know what `@value` or `@key-display` would be when your script is running? You'll have to actually put in the string value there to get the correct element, not the variable name that is in your .cshtml – mrfreester Feb 20 '18 at 18:54
  • Yeah, the other elements contain the same pattern.. – Idon'tknowhowmycodeworks Feb 20 '18 at 18:54
  • I think the best solution is finding by partial link text... thank you @Andersson – Idon'tknowhowmycodeworks Feb 20 '18 at 18:55
  • mrfreester, the value and key-display will change periodically, I was hoping there would be a way to test by variable name instead of the string value – Idon'tknowhowmycodeworks Feb 20 '18 at 18:57
  • @Idon'tknowhowmycodeworks makes sense. like you said, if there is no containing element in the hierarchy you can use to uniquely identify it, then @Andersson's would be the best since you can always look for that word in the result. Unless `gallons` appears in some other link on the page, in that case you can essentially do the same thing including it as part of an XPath – mrfreester Feb 20 '18 at 19:03

2 Answers2

1

You will have to locate the element by the HTML in the page after it's rendered so the cshtml variable name can't be used. Having said that, you should be able to find a locator that will work. I would start with a CSS selector like

a[href='#edit']

That should work unless you have multiple edit links on the page. If that doesn't work, I would try

a[href='#edit'][id$='-display']
JeffC
  • 18,375
  • 5
  • 25
  • 47
0

To find the element and invoke click() on the element you can use either of the following Locator Strategies :

  • xpath (where ID contains -display and href is #edit)

    "//a[contains(@id,'-display') and @href='#edit']"
    

    You can be more granular adding the onclick attribute as :

    "//a[contains(@id,'-display') and @href='#edit' and starts-with(@onclick,'alter')]"
    
  • cssSelector (where ID ends with -display and href is #edit)

    "//a[id$='-display'][href='#edit']"
    

    You can be more granular adding the onclick attribute as :

    "//a[id$='-display'][href='#edit'][onclick^='alter']"
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217