通过js获取所有属性,键值对存储
from selenium import webdriver
from pprint import pprint
driver = webdriver.Firefox()
driver.get('https://stackoverflow.com')
element = driver.find_element_by_xpath('//div[@class="network-items"]/a')
attrs = driver.execute_script('var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;', element)
print(attrs)
结果:
{u'class': u'topbar-icon icon-site-switcher yes-hover js-site-switcher-button js-gps-track',
u'data-gps-track': u'site_switcher.show',
u'href': u'//stackexchange.com',
u'title': u'A list of all 132 Stack Exchange sites'}