-
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
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
You can take xpath as below
- //span[contains(text(),'Acura')] - Specific to span tag. OR
- //*[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
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
-
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')]