title: How to randomly select and click on element with selenium webdriver (Python)?
body: <p>I have to randomly select an element from todo list app and click to delete it. Basically, every element has the same selector, just index changes (e.g.//*[@class=&quot;destroy&quot;])[2]).
  I have code that generates random sentences and adds them to the app. Then I have to randomly select an element -&gt; click on it to delete and here I got stuck. Random.choice selects a random number and actually, it works. But sometimes if it randomly selects [0] the test fails and test execution takes a lot of time and I believe there is a more professional way to make a random choice. Could you please advise other possible ways?</p>
  <pre><code>from selenium import webdriver
  from selenium.webdriver.common.keys import Keys
  from selenium.webdriver.common.by import By
  from selenium.webdriver import ActionChains
  import random
  import os
   
  @keyword
      def open_browser(self):
          os.environ['MOZ_HEADLESS'] = '1'
          self.driver = webdriver.Firefox()
          self.driver.get('http://todomvc.com/examples/react/#/')
          self.driver.implicitly_wait(20)
  @keyword
      def generate_random_sentences(self, number, total_items_amount):
          words = [[&quot;One&quot;, &quot;Two&quot;, &quot;Three&quot;, &quot;Four&quot;], [&quot;red&quot;, &quot;yellow&quot;, &quot;green&quot;, &quot;blue&quot;],
                   [&quot;cats&quot;, &quot;dogs&quot;, &quot;zebras&quot;, &quot;pandas&quot;], [&quot;jumped.&quot;, &quot;danced.&quot;, &quot;wrote poetry.&quot;, &quot;cried.&quot;]]
          for i in range(int(number)):
              sentences = ' '.join([random.choice(w) for w in words])
              self.driver.find_element(By.CLASS_NAME, &quot;new-todo&quot;).send_keys(sentences, Keys.ENTER)
          total_items = self.driver.find_element(By.CLASS_NAME, &quot;todo-count&quot;)
          assert total_items.text == total_items_amount```
  
  @keyword
      def delete_random_element(self, total_items_amount):
          i = random.choice(range(51))
          list_item = self.driver.find_element(By.XPATH, '(//*[@class=&quot;view&quot;]){}'.format([i])) 
          hidden_button = self.driver.find_element(By.XPATH, '(//*[@class=&quot;destroy&quot;]){}'.format([i])) 
          actions = ActionChains(self.driver)
          actions.move_to_element(list_item)
          actions.click(hidden_button)
          actions.perform()
          total_items = self.driver.find_element(By.CLASS_NAME, &quot;todo-count&quot;)
          assert total_items.text == total_items_amount```
  </code></pre>