尝试获取按钮标签的属性时没有此类元素错误
No such element error when trying to get attribute of button tags
我正在尝试获取一些按钮标签的属性。
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。我恳请你的帮助。谢谢。
visibility_of_all_elements_located
不会真正等待 All 与传递的定位器匹配的元素变得可见。
实际上,此方法与 visibility_of_element_located
方法类似,只有一个区别:visibility_of_element_located
returns 是单个网络元素,而 visibility_of_all_elements_located
returns 是网络元素列表。
要实现您在此处寻找的内容,您可以在应用 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)
我正在尝试获取一些按钮标签的属性。
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。我恳请你的帮助。谢谢。
visibility_of_all_elements_located
不会真正等待 All 与传递的定位器匹配的元素变得可见。
实际上,此方法与 visibility_of_element_located
方法类似,只有一个区别:visibility_of_element_located
returns 是单个网络元素,而 visibility_of_all_elements_located
returns 是网络元素列表。
要实现您在此处寻找的内容,您可以在应用 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)