Home > Blockchain >  How to split list of coordinates into separate variables?
How to split list of coordinates into separate variables?

Time:02-03

I'm trying to split coordinates of elements that I find like this

elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))

When I only have to find coordinates of element that appears only once I just use .location, but my problem is when I have to find element that appears more then once. I tried doing it like this but it doesn't work

elements = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))

elementlist = []

for element in elements:
    elementlocation = element.location
    elementlist.append(location)
    print(elementlist)

x,y = elementlist.split(",")
print(x,y)

I tried getting list of coordinates of element that appears multiple times, split them into separate variables x,y and print them out

CodePudding user response:

You didn't say what are your elements, but considering they are instances of some class which has location property:

element_locations = [x,y for x,y in element.location.split(",") for element in elements]
# This is gonna be a list [(x1,y1), (x2,y2), ...,(xn,yn)] 

CodePudding user response:

To print the x,y location of the elements you can use:

print([element.location for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))])
# outputs -> [{'x': 100, 'y': 367}, {'x': 100, 'y': 684}, {'x': 100, 'y': 684}, {'x': 100, 'y': 684}, {'x': 100, 'y': 1917}]

To print only the coordinates:

print([(element.location['x'],element.location['y']) for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[text() ='100']")))])
# outputs -> [(100, 367), (100, 684), (100, 684), (100, 684), (100, 1917)]
  • Related