1

I am creating some test scripts using Selenium WebDriver in C#. The AUT contains a left menu in which the position/ordering of the links depend upon the 'User Preferences' and a lot of factors/configuration from Admin and user side, so XPath is not an option for me.

I used IDE for recording the same and then generating the code, but by default IDE also used XPath, so I added some commands by myself in recorded script and tried to find the element, with the 'Find' button provided in Selenium IDE. After a couple of tries it worked in IDE, i.e. IDE highlighted the element which I want to find, but when I copied the same code in VSTS it is not working.

This is the code generated by IDE, I am not able to get the reason why this is working in IDE and not in VSTS code, I haven't seen any other command to show such a behavior.

driver.FindElement(By.CssSelector("span:contains(Issues Management)")).Click();

This code throws element not found/similar error when executed in WebDriver but works fine in IDE. I have seen a similar post over SQAForums, but that too doesn't explain it's reason well. I am using Firefox as a browser for creating these tests.

HTML of the element is:

<a class="rpLink rpExpandable" title="MyServices" href="#">
<span class="rpOut">
<span class="rpExpandHandle"></span>
<span class="rpText">
Issues Management
<span class="menuItemRole">Container menu item</span>
</span>
</span>
</a>

The complexity is there can be multiple elements with the same text 'Issues Management' on the page, but as far as I know they will be under a different section/class name.

  • How about asking a developer to provide static IDs for those links in left menu? There is a great benefit from this as you will always be able to interact with web element no matter what the position of this link compare to others. – Tired Of Testing Oct 2 '17 at 19:04
1

"contains" isn't a valid css selector, so it won't work (see this CSS Selector Reference). I read somewhere it was part of a draft version of CSS once, but got cut (see here).

Xpath may still be an option; even if the order of elements on the page is uncertain, you can use a partial xpath

driver.findElement(By.xpath("//span[@class='rpText' and text()='Issues Management']")).click();

You can also chain findElements to narrow things down if that doesn't work for some reason

driver.findElement(By.CssSelector("a[title='MyServices']")).findElement(By.xpath(".//span[text()='Issues Management']")).click();
1

Use [0] for the first instance

This may be due to the seleniumIDE selecting a first instance by default You can achieve this manually with:

CssSelector("span.rpText:contains(Issues Management)")[0]

Also, if the class is unique then use it in the selector, i.e.

css:

driver.
  FindElement(By.CssSelector("span.rpText:contains(Issues Management)")).Click();

xpath:

driver.findElement(By.xpath(
"/descendant::a[@class='post-rpText'][2]:contains('Issues Management')")).click();
  • Already mentioned that 'contains' is not working in WebDriver :( – Dhiman Nov 10 '15 at 16:26
  • Actually you didn't specifically mention contains, just the code is not working. Also you didn't use the rptText class in your example so my example is different. Not trying to be annoying, just what I found. – Michael Durrant Nov 10 '15 at 19:36
  • You are right, that I didn't mentioned this statement, my mistake :). I will try your solution too. – Dhiman Nov 10 '15 at 19:59
0

Try below XPath:

//span[contains(text(), 'Issues Management')]

Might be it will help you out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.