Home > Enterprise >  How to select random values from the list?
How to select random values from the list?

Time:01-11

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("123456");

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.

CodePudding user response:

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

CodePudding user response:

You can try the below code:

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

# click Edit Appointment Hours button
Button = driver.find_element(By.XPATH,"//*[text()=' Edit Appointment Hours ']")
Button.click()
time.sleep(1)

# click Select Outlet dropdown
element = driver.find_element(By.CSS_SELECTOR,".multiselect.col-md-4 .multiselect__tags")
driver.execute_script("arguments[0].scrollIntoView();", element)
time.sleep(1)
element.click()

outlet_options = driver.find_elements(By.XPATH,".//*[contains(@class,'multiselect col-md-4')]//span[@class='multiselect__option']")

option_to_select = random.randint(1, len(outlet_options) - 2)
print("option to select in Select outlet - ", option_to_select)

driver.find_element(By.XPATH, "(.//*[contains(@class,'multiselect col-md-4')]//span[@class='multiselect__option'])["   str(option_to_select)   "]").click()

time.sleep(1)

time_to_select = random.randint(1, 3)
print("time to select: ", time_to_select)
select = Select(driver.find_element(By.XPATH,".//*[@name='predefinedHours']"))
select.select_by_index(time_to_select)
time.sleep(1)

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