我正在嘗試獲取一些按鈕標簽的屬性。
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/div/button"))
)
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')
for item in li_items:
first = item.find_element(By.TAG_NAME, 'div')
second = first.find_element(By.TAG_NAME, 'button')
coord = second.get_attribute("onclick")
print(coord)
我正在等待所有按鈕元素加載。但是,當我嘗試列印onclick所有按鈕標簽的屬性時,它只列印前兩個,然后拋出“NoSuchElementException:訊息:沒有這樣的元素:無法定位元素”。但我不知道為什么。您可以通過轉到“https://migroskurumsal.com/magazalarimiz/”并從最右側的下拉選單中選擇“Migros”來查看 html。我懇請您的幫助。謝謝你。
uj5u.com熱心網友回復:
visibility_of_all_elements_located不會真正等待與傳遞的定位器匹配的所有元素變得可見。
實際上,此方法與方法相似visibility_of_element_located,只有一個區別:visibility_of_element_located回傳單個 Web 元素,而visibility_of_all_elements_located回傳 Web 元素串列。
為了實作您在此處查找的內容,您可以在應用該visibility_of_all_elements_located方法后添加 1 秒的短暫延遲,以讓所有 Web 元素被加載。
請試試這個:
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/div/button")))
time.sleep(1)
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')
for item in li_items:
first = item.find_element(By.TAG_NAME, 'div')
second = first.find_element(By.TAG_NAME, 'button')
coord = second.get_attribute("onclick")
print(coord)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/414624.html
標籤:
