英文:
Selenium Python: The second loop in the program isn't geting executed
问题
我的代码在达到第二个循环时不起作用。当我悬停在第一个类别上时,它显示第二个类别,我需要悬停在第二个类别上才能看到第三个类别。以下是我的代码:
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()
英文:
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()
答案1
得分: 0
首先,要抓取网站,使用Beautiful Soup(bs4)和对列表进行迭代似乎是一个更好的方法。
现在,find_elements
返回一个列表。在你的第二个for循环中,你正在迭代只有一个值的列表。当我检查页面时,我注意到活动的子菜单或子子菜单被分配了类lzd-site-menu-sub-active
和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()
这段代码成功地悬停在第三级上,正如你所看到的。然而,由于CSS选择器不太好,它有点无用。希望这对你有所帮助。
英文:
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.
答案2
得分: 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
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论