Selenium webdriver: How do I find ALL of an element's attributes?

后端 未结 4 1086
[愿得一人]
[愿得一人] 2020-11-30 23:44

In the Python Selenium module, once I have a WebElement object I can get the value of any of its attributes with get_attribute():

f         


        
4条回答
  •  攒了一身酷
    2020-11-30 23:55

    You can find using element.get_property() method.

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    
    driver = webdriver.Chrome()
    driver.get("https://www.ultimateqa.com/complicated-page/")
    
    logo = driver.find_element(By.XPATH, "//img[@id='logo']")
    attrs=[]
    for attr in logo.get_property('attributes'):
        attrs.append([attr['name'], attr['value']])
    print(attrs)
    

    Output:

    [['src', 'https://www.ultimateqa.com/wp-content/uploads/2019/01/horizontal_on_transparent_by_logaster-2.png'], ['alt', 'Ultimate QA'], ['id', 'logo'], ['data-height-percentage', '100'], ['data-actual-width', '912'], ['data-actual-height', '410']]
    

提交回复
热议问题