【问题标题】:No such element error when trying to get attribute of button tags尝试获取按钮标签的属性时没有此类元素错误
【发布时间】:2022-01-17 21:38:57
【问题描述】:

我正在尝试获取一些按钮标签的属性。

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。我恳请您的帮助。谢谢。

【问题讨论】:

    标签: python html selenium webdriverwait


    【解决方案1】:

    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)
    

    【讨论】:

    • 大多数情况下超时异常表示定位器错误。
    • 现在不给出超时异常,但仍然打印前两个属性,找不到其余的。
    • 我在这里看到了太多的问题...首先不是所有的li标签元素都展示了商店,每个商店块包含2个//ul[@id='shopList']/li/div/button元素,你必须滚动页面为了访问元素等...
    • 你知道如何在定位元素时向下滚动吗?
    • 当然,但不是在定位时。您可以获取元素列表,然后滚动到列表中的每个或任何元素。但这是一个额外的问题。我想我已经回答了你原来的问题。如果是这样,请接受它,如果您想获得任何其他特定问题的帮助,请为每个特定问题打开一个新的单独问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-08
    • 2018-12-13
    相关资源
    最近更新 更多