views:

58

answers:

1

Lets say I have a list of links and want to click a link at random:

<div id="divA">
   <a> first link </a>
   <a> second link </a>
   ...
</div>

It isn't the smartest of ways (and if you have a better solution please tell me) but what I currently do is (roughly):

l = []
for i in range(numOfLinks):
    xpath = '//div[@id="divA"]/a[%d]'%i
    txt = sel.getText(xpath)
    l.append(xpath, txt)

xpath,linkName = random.choice(l)
sel.click(xpath)

The main problem of this solution is that it sends many requests to selenium. My question is: is there a way of saving all these requests in a buffer and sending them at once?

A: 

are you using the text for anything?

numOfLinks = sel.get_xpath_count('//div[@id="divA"]/a')
random.randrange(1,numOfLinks)
sel.click('//div[@id="divA"]/a[%d]'%random.randrange(1,numOfLinks))

The code above will always click on a random link without having to get the text of the link each time.

AutomatedTester
Right, but this only solves this situation. If you just have many `sel.get_text` on one page. A buffer would still come in handy
Guy
As far as I can recall there isn't something like that in the api however you can make a single call into the page with getEval and tell it to collect the data you are after in the DOM and return the results
AutomatedTester
that's a bit crooked..
Guy