0

I have the following Xpath:

'.//*[@id="reporting_usn_browser_tab_source_tree_5_crm_f1/5_address"]'

I would like to use ends with as I would like to find the text address which is at the end of the string

I have tried:

'.//*[@id="reporting_usn_browser_tab_source_tree_5_crm_f1/5_address"][ends-with(@id, 'address']

The HTML is:

<div>
    <span id="reporting_usn_browser_tab_source_tree_5_crm_f1/3_address" class="" title="" style="">address</span>
</div>

My function checks if the address element is not on the page. The element is not on the page so I would like to return True.

def is_address_data_object_element_value_not_displayed_from_expanded_tree(self ): # Check if the address data object value is not displayed. For dataset ID f1/3 the address data object value should not be displayed.
    #data_objet_address_element = self.get_element(By.XPATH, './/*[ends-with(@id, "address")]')
    data_object_address_element = self.get_element(By.XPATH, '.// *[substring( @ id, string - length( @ id) - string - length("address") + 1) = "address"]')
    if data_object_address_element.text is None or data_object_address_element == "":
        return True

I get the error:

 Unable to locate an element with the xpath expression .//*[ends-with(@id, "address")] because of the following error: TypeError: Unable to get property 'I' of undefined or null reference

I have also tried:

data_object_address_element = self.get_element(By.ID, '.// *[substring( @ id, string - length( @ id) - string - length("address") + 1) = "address"]')

I get the error:

NoSuchElementException: Message: Unable to find element with id == .// *[substring( @ id, string - length( @ id) - string - length("address") + 1) = "address"]

What is the Xpath syntax to use ends-with please?

Thanks, Riaz

Riaz Ladhani
  • 3,946
  • 15
  • 70
  • 127
  • Possible duplicate of [How to use starts-with() , contains() and ends-with() in XPath to find the xml node innertext? in XPATH 1.0](http://stackoverflow.com/questions/26217002/how-to-use-starts-with-contains-and-ends-with-in-xpath-to-find-the-xml-n) – JeffC Sep 20 '16 at 19:46

1 Answers1

0

try the below xpath ,

.//*[substring(@id, string-length(@id) - string-length('address') +1) = 'address']
Sudharsan Selvaraj
  • 4,792
  • 3
  • 14
  • 22
  • I get the error: InvalidSelectorException: Message: Unable to locate an element with the xpath expression .//*[ends-with(@id, "address")] because of the following error: TypeError: Unable to get property 'I' of undefined or null reference – Riaz Ladhani Sep 21 '16 at 09:59
  • The ends-with function is part of xpath 2.0 but browsers generally only support 1.0. So updated my answer. – Sudharsan Selvaraj Sep 21 '16 at 10:02
  • I still get the same error. Ah in the Firefox browser Xpath checker it locates it but in my Python code the error will show – Riaz Ladhani Sep 21 '16 at 10:03
  • its working perfectly for me. can you post the code? – Sudharsan Selvaraj Sep 21 '16 at 10:08
  • I think I know why, I am asserting if the address element is on the page or not. The address element is not on the page. I want to return true if the element is not there. I have added my method above in my question. – Riaz Ladhani Sep 21 '16 at 10:09
  • Well in Xpath checker in Firefox browser it works. In my Python Selenium code it is not working. My Function call is above in my question. – Riaz Ladhani Sep 21 '16 at 10:17