Search code examples
pythonseleniumdrop-down-menu

Unable to select the element in a 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("xxxxx");

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)

# sub-category button
subcategory_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[4]/div/div/div/div[3]")
# An "Category 4 - HairCut" item in list of categories
subcategory_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[4]/div/div/div/div[3]/ul/li[2]""")
# Click category button to show list.
subcategory_button.click()
# Click on category you want select.
subcategory_select.click()
time.sleep(3)

When I moving into the next part which is selecting a value(random from the list), but it said the element is not interactable. I tried with the click function and driver.implicity_wait. Appreciate if someone could help. Thank in advance. The link is https://devbusiness.tunai.io/dashboard/salon_menu_service username and pass can find in the code.


Solution

  • In this case, the error element is not interactable appear because you try to click the element having display: none; attribute. You need trigger the element with clicking related element first to make visible, and then scroll too.

    ....
    ....
    
    # trigger with other element first, add this code
    element = driver.find_element(By.XPATH,"//span[text()='No Sub-Category']")
    driver.execute_script("arguments[0].scrollIntoView();", element)
    time.sleep(1)
    element.click()
            
    # sub-category button
    subcategory_button = self.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[4]/div/div/div/div[3]")
    # An "Category 4 - HairCut" item in list of categories
    subcategory_select = self.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[4]/div/div/div/div[3]/ul/li[2]""")