4

I'm having trouble with getting a dynamic element from a website, basically I'm getting input from my user which is later used as input into a website, the name of the element which I want to get afterwards changes based on the input that i got earlier. Therefore the class that i want to find has two possible names, either label label-success or label label-danger. Right now this is the code:

for elem in browser.find_elements_by_xpath('.//span[@class = "label label success"]'):
    print elem.text

But whenever i get input from the user that changes the class name to label label-danger I am getting below error

StaleElementReferenceException: Message: {"errorMessag "Element does not exist in cache}

my question is: is it possible to do something like:

find_elements_by_xpath[@class = "label label-success" OR @class = "label label-danger"]

if so how? Or is there any other simple solution to this problem? Thank you

Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137
Dantuzzo
  • 271
  • 6
  • 25
  • what is the error you are getting ? what you suggest is possible – Gaurang Shah Oct 11 '17 at 09:58
  • raise exception_class(message, screen, stacktrace) StaleElementReferenceException: Message: {"errorMessage":"Element does not exist in cache" – Dantuzzo Oct 11 '17 at 10:09
  • your second xpath is valid, the error you are getting has nothing to do with xpath, I have pointed out the solution for the error in answers – Gaurang Shah Oct 11 '17 at 10:42
  • Possible duplicate of [Using an OR condition in Xpath to identify the same element](https://stackoverflow.com/questions/34523638/using-an-or-condition-in-xpath-to-identify-the-same-element) – JeffC Oct 11 '17 at 13:38

3 Answers3

2

Alternatively, you should be able to use the XPath OR operator |, for instance:

find_elements_by_xpath('.//span[@class = "label label success"|@class = "label label-danger"]')
Andreas
  • 221
  • 1
  • 11
1

You can try separating them into two list one for "label label-success" and other for "label label-danger" and then iterate them differently . Or Like this

for elem in itertools.chain(browser.find_elements_by_xpath('.//span[@class = "label label-success"]'), browser.find_elements_by_xpath('.//span[@class = "label label-danger"]')):
print elem.text
0

The reason you are getting StaleElementReferenceException is a below.

You are creating a list of WebElements and trying to iterate through that. However by the time you collected list of WebElement (below code)

browser.find_elements_by_xpath('.//span[@class = "label label success"]'):

And trying to some action on it (below code)

  print elem.text

Something is changing on the page. ( it might not be visible) and so webdriver is telling you that this element is stale.

Following code my fix the issue.

total_elements = len(browser.find_elements_by_xpath('.//span[@class = "label label success"]'))
for index in range(total_elements):
    print browser.find_elements_by_xpath('.//span[@class = "label label success"]')[index].text
Gaurang Shah
  • 11,764
  • 9
  • 74
  • 137