1

How can I get the highlighted value using Selenium C#? enter image description here

  • Thanks everyone for your responses!! Is there a way I can search for it without using a hard-coded value like the contains(text) & store it in a variable? this is because the text values will change. – selenium test Nov 1 '18 at 19:52
1

Try with:

//*[contains(text(),'Acura')]

  • Thanks for your response!! Is there a way I can search for it without using a hard-coded value like the contains(text) & store it in a variable? this is because the text values will change – selenium test Nov 2 '18 at 13:19
1

You can take xpath as below

  1. //span[contains(text(),'Acura')] - Specific to span tag. OR
  2. //*[contains(text(),'Acura')] - Generic with any tag.

You can directly inspect your locators by using Chropath plugin in Firefox or you can use internal inspector in Chrome.

If you are getting duplicate xpath with above so you can use following-sibling, preceding-sibling or XPath Axes to get unique xpath.

Hope this will help you!!

  • Thanks for your response!! Is there a way I can search for it without using a hard-coded value like the contains(text) & store it in a variable? this is because the text values will change – selenium test Nov 2 '18 at 13:18
0

Xpath selector:

driver.find_elements_by_xpath("//*[contains(text(), 'Acura West')]")

or css selector:

driver.find_elements_by_css_selector("div.ng-scope + span")

EDIT:

For more flexible selection use:

string name = "Acura West";
driver.find_elements_by_xpath("//*[contains(text(), '{name}')]")
  • Thanks for your response!! Is there a way I can search for it without using a hard-coded value like the contains(text) & store it in a variable? this is because the text values will change – selenium test Nov 2 '18 at 13:18
  • You can do that. Use string interpolation. – Mate Mrše Nov 2 '18 at 13:34
0

If you don`t want to search for a particular text, but rather for the element (as the text might change based on the listing/page), based on the HTML snippet you could use XPath such as:

# Matches elements with "item.name" in value of one of its attributes
//span[starts-with(@data-ng-bind, 'item.name')]

or

# Same as before, except it`s exact match of the particular attribute`s value
//span[contains(@data-ng-bind, 'item.name|wrapText')]

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.