I am trying to get li items in a ul. Here is my code:
driver.get('https://migroskurumsal.com/magazalarimiz/')
try:
select = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'stores'))
)
print('Dropdown is ready!')
except TimeoutException:
print('Took too much time!')
select = Select(driver.find_element(By.ID,'stores'))
select.select_by_value('2')
try:
shopList = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "shopList"))
)
print('Shoplist is ready!')
except TimeoutException:
print('Took too much time!')
driver.quit()
print(shopList.get_attribute("class"))
li_items = shopList.find_elements(By.TAG_NAME,'li')
print(len(li_items))
I located the ul element with id=shopList successfully. Then I tried to get all of the li elements under ul by using find_elements(By.TAG_NAME). I also tried find_elements(By.CLASS_NAME), however the len(li_items) is always 0. I kindly request your help. Thank you.
CodePudding user response:
In your case waiting until EC.presence_of_element_located((By.ID, "shopList")) does not do the trick, as this ul is already on the page, even before selecting filters, it's just empty. Instead, you could wait until li child elements are visible.
Example:
driver.get('https://migroskurumsal.com/magazalarimiz/')
try:
select = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, 'stores'))
)
print('Dropdown is ready!')
except TimeoutException:
print('Took too much time!')
select = Select(driver.find_element(By.ID, 'stores'))
select.select_by_value('2')
shopList = driver.find_element(By.ID, "shopList")
try:
WebDriverWait(driver, 10).until(
EC.visibility_of_all_elements_located((By.XPATH, "//ul[@id='shopList']/li"))
)
print('Shoplist is ready!')
except TimeoutException:
print('Took too much time!')
driver.quit()
print(shopList.get_attribute("class"))
li_items = shopList.find_elements(By.TAG_NAME, 'li')
print(len(li_items))
Results with output:
Dropdown is ready!
Shoplist is ready!
shop-list list-unstyled
601
