Skip to content
Advertisement

How to select random values from the list?

from random import Random
from selenium import webdriver  
import time
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select

driver = webdriver.Chrome('chromedriver')
driver.get('https://devbusiness.tunai.io/login')
time.sleep(2)
driver.maximize_window()

# Create variables for login credentials.
username = driver.find_element(By.NAME, "loginUsername");
username.send_keys("kevin@tunai");

password = driver.find_element(By.NAME, "loginPassword");
password.send_keys("xxxxx");

login = driver.find_element(By.XPATH,"//*[@id='app']/div/div/div/div/div/div[2]/form/div[4]/button");
login.submit();
time.sleep(1)

driver.get("https://devbusiness.tunai.io/dashboard/my_salon_appointment")
time.sleep(1)

Button = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div/div/div[1]/div/div[1]/button")
Button.click()
time.sleep(1)

# trigger with other element first, add this code
element = driver.find_element(By.XPATH,"//*[@id='edit-hours']/div/div/div[2]")
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(1)
element.click()

# outlet button
outlet_button = driver.find_element(By.XPATH,"//*[@id='edit-hours']/div/div/div[1]")
# An kevin@tunai in the
outlet_select = driver.find_element(By.XPATH,"""//*[@id="edit-hours"]/div/div/div[3]/ul/li[2]/span""")

# Click category button to show list.
outlet_button.click()
# Click on category you want select.
outlet_select.click()
time.sleep(3)

select = Select(driver.find_element(By.XPATH,"//*[@id='edit-hours']/div[1]/select"))
select.select_by_index(3)
time.sleep(3)

driver.find_element(By.XPATH,"//*[@id='edit-hours']/div[4]/button").click()

I am trying to perform a web automation testing, and now i am stucking at how to select random values from the outlet and time list. Kinda ran out of idea, hope someone could help, Thanks in Advance and Have a Nice day.

Advertisement

Answer

As far as I understand the code above, something like this should work:

from random import choice
# pass here a results from select
list_of_select_objects = ['obj1', 'obj2', 'obj3']
# creates a list of indexes
list_of_indexes = list(range(len(list_of_select_objects)))

random_index = choice(list_of_indexes)
# you get a random index
select.select_by_index(random_index)
# use it to select a random element
Advertisement