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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
driver = webdriver.Chrome('chromedriver')
driver.get('https://devbusiness.tunai.io/login')
time.sleep(2)
driver.maximize_window()
# Create variables for login credentials.
username = "your username"
password = "your password"
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(2)
# Wait for login process to complete.
WebDriverWait(driver=driver, timeout=10).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
# Verify that the login was successful.
error_message = "Incorrect username or password."
# Retrieve any errors found.
errors = driver.find_elements(By.CLASS_NAME, "flash-error")
# When errors are found, the login will fail.
if any(error_message in e.text for e in errors):
print("[!] Login failed")
else:
print("[ ] Login successful")
driver.get("https://devbusiness.tunai.io/dashboard/salon_menu_service")
service = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/button")
service.click();
driver.find_element(By.TAG_NAME,"input").send_keys("Hair Dying")
price = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[1]/div/div/input")
price.clear()
price.send_keys("50")
baseprice = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[2]/div/div/input")
baseprice.clear()
baseprice.send_keys("10")
category = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[2]")
Select.select_by_visible_text("Category 2 - BeautyPOS")
time.sleep(3)
I wish to select one of the values from the drop down list but it didn't work when i using "select" function.I couldn't figure it out what's the issues. Been tried many ways to debug it but failed...Appreciate if anyone could help,Thanks in advance.
CodePudding user response:
I think I found a way how to select category in your styled list.
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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.remote.webelement import WebElement
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(2)
# Wait for login process to complete.
WebDriverWait(driver=driver, timeout=10).until(
lambda x: x.execute_script("return document.readyState === 'complete'")
)
# Verify that the login was successful.
error_message = "Incorrect username or password."
# Retrieve any errors found.
errors = driver.find_elements(By.CLASS_NAME, "flash-error")
# When errors are found, the login will fail.
if any(error_message in e.text for e in errors):
print("[!] Login failed")
else:
print("[ ] Login successful")
driver.get("https://devbusiness.tunai.io/dashboard/salon_menu_service")
service = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/button")
service.click();
driver.find_element(By.TAG_NAME,"input").send_keys("Hair Dying")
price = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[1]/div/div/input")
price.clear()
price.send_keys("50")
baseprice = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[2]/div[2]/div/div/input")
baseprice.clear()
baseprice.send_keys("10")
# Category button
category_button = driver.find_element(By.XPATH,"//*[@id='page-content']/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[2]")
# An "Category 2 - BeautyPOS" item in list of categories
category_select = driver.find_element(By.XPATH,"""//*[@id="page-content"]/div/div[2]/div[1]/div[1]/div/div[2]/div/div/form/div[1]/div[1]/div/div[1]/div[3]/div/div/div/div[3]/ul/li[2]/span""")
# Click category button to show list.
category_button.click()
# Click on category you want select.
category_select.click()
time.sleep(3)