selenium 如何从列表中选择随机值?

uz75evzq  于 2023-01-13  发布在  其他
关注(0)|答案(2)|浏览(158)
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()

我正在尝试执行一个网络自动化测试,现在我正纠结于如何从插座和时间列表中选择随机值。有点想不出办法了,希望有人能帮忙,提前感谢,祝你有美好的一天。

6ovsh4lw

6ovsh4lw1#

就我对上面代码的理解而言,应该是这样的:

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
jchrr9hc

jchrr9hc2#

你可以试试下面的代码:

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()

相关问题