Question

Hello stackoverflow users. Today i want to ask for your help with something what is only a small part of my project. So... I have already done small application using Selenium to get text from div, text is not static, it changes with the character moves. Let me show:

botloc = driver.find_element_by_id('botloc').text
    print botloc

I already looped it, so i can get update every 0,5sec (character moves 1 square within half a second). Of course i know its better to bind with key, but loop works fine for me when its in testing phase.

[example] Output of one botloc looks like that:

26,20 

This is x and y from actuall position, so I want to use that comma like a separator to separate x and y, then i can assign each of them to single name(def, class, whatever).

[example]Output of 5 x botloc when I move my character looks like that:

28,20
28,21
28,22
29,22
30,22

So basically, it's all about square map, but i dont know the idea how to use that, so i created my own idea with simple logic and mathematics.

I am also trying to understand information from here: http://docs.python.org/2/tutorial/inputoutput.html but for now I wants to do homework is described above.

I was trying to use:

firstpart, secondpart = botloc[:len(botloc)/2], botloc[len(botloc)/2:]

But after including it to my code my botloc is not working (no error, no text, just white), and the separator too, is any possibility to fix that?

Thanks for your understanding for me-new python learner!

Was it helpful?

Solution

>>> botloc="28,20"
>>> botX,botY = map(int,botloc.split(','))
>>> botX,botY
(28, 20)

If you wanted to store multiple bot locations you could do this:

>>> botLocations = []
>>> botLocations.append(map(int,botloc.split(',')))
>>> botLocations
[[28, 20]]

Where each time yout get a new botloc you split it by commas, convert each value seperated into an int, and store it inside botLocations.


botloc[:len(botloc)/2], botloc[len(botloc)/2:]

Wouldn't work if botLoc was uneven i.e 198,2. Instead you would have to do something like

>>> botloc="28,20"
>>> botloc[:botloc.index(',')]
'28'
>>> botloc[botloc.index(',')+1:]
'20'

To seperate the string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top