0

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?

4

1 回答 1

0

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.

于 2010-01-01T14:53:29.223 回答