我正在嘗試在 ul 中獲取 li 專案。這是我的代碼:
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))
我成功找到了 id=shopList 的 ul 元素。然后我嘗試使用 find_elements(By.TAG_NAME) 獲取 ul 下的所有 li 元素。我還嘗試了 find_elements(By.CLASS_NAME),但是 len(li_items) 始終為 0。我請求您的幫助。謝謝你。
uj5u.com熱心網友回復:
在您的情況下,等待直到EC.presence_of_element_located((By.ID, "shopList"))不會起作用,因為這ul已經在頁面上,即使在選擇過濾器之前,它也是空的。相反,您可以等到li子元素可見。
例子:
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))
輸出結果:
Dropdown is ready!
Shoplist is ready!
shop-list list-unstyled
601
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/411718.html
標籤:
