-1

Using the command:

AnyNumber = driver.find_elements_by_xpath('//*[starts-with(@id, "popover-")]')
for list_AnyNumber in AnyNumber:
    print(int(list_AnyNumber.text))

I get 10 numbers (for example 10,20,30, etc.) How can I check that each next element is greater than the previous one?

imho
  • 75
  • 7

6 Answers6

0
AnyNumber = driver.find_elements_by_xpath('//*[starts-with(@id, "popover-")]')
for index, list_AnyNumber in enumerate(AnyNumber):
    if index > 0:
        print(int(AnyNumber[index].text) > int(AnyNumber[index-1],text))

Or you can change the foreach loop to for in range()

RnD
  • 1,004
  • 4
  • 22
  • 49
  • gives an error message for index, `list_AnyNumber in enumerate(b1Text): TypeError: 'int' object is not iterable` – imho Mar 19 '19 at 08:01
0

Take the length count and use for loop and if conditions.Hope this help.

AnyNumber = driver.find_elements_by_xpath('//*[starts-with(@id, "popover-")]')
ncount=len(AnyNumber)
for l in range(ncount-1):
    if int(AnyNumber[l+1].text) > int(AnyNumber[l].text):
     print(int(AnyNumber[l+1].text))
imho
  • 75
  • 7
KunduK
  • 29,126
  • 4
  • 11
  • 33
  • the code stops there : `ncount=len(list)` and gives an error `TypeError: object of type 'type' has no len()` – imho Mar 19 '19 at 07:59
  • Sorry my mistake this should be ncount=len(AnyNumber) typo error. Change list to AnyNumber and try – KunduK Mar 19 '19 at 08:26
  • I changed your code a little http://joxi.ru/J2bj9OQt0XRPG2 , But now I do not understand the logic of the script. For some reason he writes OK and ERROR although looking at the numbers, each number is less than the other. – imho Mar 19 '19 at 16:36
  • @keepomen : your code is totally wrong.Can post here in comment box so that i can modify – KunduK Mar 19 '19 at 16:41
  • I can not properly insert the code. It turns out in one line without indents and hyphens.... xPath = "//*[starts-with(@id, 'td_')]/td[3]" elements = driver.find_elements_by_xpath(xPath) for element in elements: b1Text = driver.execute_script("return arguments[0].childNodes[2].textContent", element); b1Text = b1Text.replace("'", "").replace(" ", "") ncount = len(b1Text) for l in range(ncount - 1): if int(b1Text[l + 1]) > int(b1Text[l]): print('OK') else: print('ERROR') – imho Mar 19 '19 at 16:42
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/190316/discussion-between-kajal-kundu-and-keepomen). – KunduK Mar 19 '19 at 16:49
0

Here's one interesting approach. Say your list is called any_number_list. Then You could use following syntax:

sequence = [y - x for x, y in zip(any_number_list[:-1], any_number_list[1:]) if y - x < 0] 
if not sequence:
    print("Each element is greater than the previous one")

You pack two arrays using zip and iterate through them, adding difference to new array called sequence if difference is less than 0. If, at the end, this array contains any number, then this is not monotonously non-falling sequence.

Fejs
  • 2,608
  • 2
  • 19
  • 37
  • `TypeError: 'int' object is not subscriptable` – imho Mar 19 '19 at 08:02
  • You do not work with the list than, but with the single item (which is `int`). Read the second sentence in my answer, it is assumed that You have a list. I suppose You know how to create one. – Fejs Mar 19 '19 at 08:05
0
AnyNumber=['14','22','33', '41', '55']
x=len(AnyNumber)
for index, list_AnyNumber in enumerate(AnyNumber):
    if index < (x-1):
        if int(AnyNumber[index + 1]) > int(AnyNumber[index]):
            print(str(AnyNumber[index + 1]) + ">" + str(list_AnyNumber))

Based off of this users answer

AG-W
  • 70
  • 1
  • 9
0

The easiest way is to sort the list and then compare to the original list. They should match if the numbers increment as expected.

The first thing you need to do is to convert the list of string to a list of int, then sort, then compare.

A simple example,

list = ['10', '20', '30', '40', '50']
int_list = [int(i) for i in list]
print(int_list == sorted(int_list))

This will print True because the original list is sorted.

To see a failed case,

list = ['50', '20', '30', '10', '40']
int_list = [int(i) for i in list]
print(int_list == sorted(int_list))

Your code will look something like

list = driver.find_elements_by_xpath('//*[starts-with(@id, "popover-")]')
int_list = [int(i.text) for i in list]
print(int_list == sorted(int_list))
JeffC
  • 19,228
  • 5
  • 29
  • 49
  • numbers in elements change dynamically – imho Mar 19 '19 at 16:08
  • Yep... this answer will work with dynamic values. The first two are just examples to show you how it works. The final code will work in your case. If the values aren't already sorted, the sorted list will not match the original and will print `False`. – JeffC Mar 19 '19 at 17:19
  • understand that in your example, sorting from large to small. And if you need the opposite? – imho Mar 20 '19 at 06:45
  • In my passing example, the numbers would be sorted in ascending order. If you needed descending, it is covered in [the docs](https://docs.python.org/3/howto/sorting.html#ascending-and-descending). – JeffC Mar 20 '19 at 13:11
0

I found the solution that came up on another forum

AnyNumber = driver.find_elements_by_xpath('//*[starts-with(@id, "popover-")]')
buffer = 0
for AnyNumbers in AnyNumber:
    if int(AnyNumbers) > buffer:
        print('more than')
    elif int(AnyNumbers) < buffer:
        print('less than')
    else:
        print('is')
    buffer = int(AnyNumbers)
imho
  • 75
  • 7