1

my code not working when reaching the second loop. when I hover over the first category it's showing the second category and I need to hover the second category to see the third category. here is my code:

driver.get("https://www.daraz.com.bd/")
main_category = driver.find_elements(By.CSS_SELECTOR , '.lzd-site-menu-root-item span')
for i in main_category:
    hover = ActionChains(driver).move_to_element(i)
    hover.perform()
    time.sleep(1) 
    sub_category_one = driver.find_elements(By.CSS_SELECTOR , ".Level_1_Category_No1 [data-spm-anchor-id] span")
    for i in sub_category_one:
             hover = ActionChains(driver).move_to_element(i)
             hover.perform()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
boyenec
  • 1,405
  • 5
  • 29

2 Answers2

0

First of to scrape the site, bs4 and iterating over the lists seems like a much better approach.

Now find_elements returns a list. You are iterating over a list with only one value in your second for loop. When I inspected the page I noticed that a submenu or subsubmenu that is active is assigned the class lzd-site-menu-sub-active and lzd-site-menu-grand-active.

main_category = driver.find_elements(By.CSS_SELECTOR, ".lzd-site-menu-root-item span")
for main in main_category:
    ActionChains(driver).move_to_element(main).perform()

    sub_category = WebDriverWait(driver, 3).until(
        lambda x: x.find_elements(By.CSS_SELECTOR, ".lzd-site-menu-sub-item span")
    )
    for sub in sub_category:
        ActionChains(driver).move_to_element(sub).perform()

        subsub_category = WebDriverWait(driver, 3).until(
            lambda x: x.find_elements(By.CSS_SELECTOR, ".lzd-site-menu-grand-item span")
        )

        for subsub in subsub_category:
            ActionChains(driver).move_to_element(subsub).perform()

This code manages to go hover over the third level as you will see. However because of the bad CSS_Selector it's somewhat useless. I hope this could be of help.

SirLoort
  • 1
  • 1
0

To Mouse Hover over the first category which shows the second categories and then to Hover over the second categories to see the third categories you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following locator strategies:

  • Code block:

    driver.get('https://www.daraz.com.bd/')
    main_category = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".lzd-site-menu-root-item span")))
    for i in main_category:
        ActionChains(driver).move_to_element(i).perform()
        time.sleep(1) 
        sub_category_one = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "ul.Level_1_Category_No1 li.lzd-site-menu-sub-item")))
        for j in sub_category_one:
            ActionChains(driver).move_to_element(j).perform()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352