/
ilya.petrash
/
SimplePyScripts
Обзор
Документация
Войти
/
ilya.petrash
/
SimplePyScripts
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
selenium__examples/get_attributes.py
38 строк
1 KB
gil9red
Refactoring. Replacing deprecated "find_element_*" methods of selenium
26 июн 2023, 21:05
26 июн 2023, 21:05
16c596e
Код
Авторство
О чём код?
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = "ipetrash" # pip install selenium from selenium import webdriver from selenium.webdriver.common.by import By def get_attributes(driver, element) -> dict: return driver.execute_script( """ let attr = arguments[0].attributes; let items = {}; for (let i = 0; i < attr.length; i++) { items[attr[i].name] = attr[i].value; } return items; """, element, ) driver = webdriver.Firefox() driver.implicitly_wait(10) # seconds driver.get("https://ru.stackoverflow.com/") input_el = driver.find_element(By.CSS_SELECTOR, "input.s-input__search") attrs = get_attributes(driver, input_el) print(attrs) # {'aria-controls': 'top-search', 'aria-label': 'Поиск', 'autocomplete': 'off', # 'class': 's-input s-input__search js-search-field ', 'data-action': 'focus->s-popover#show', # 'data-controller': 's-popover', 'data-s-popover-placement': 'bottom-start', 'maxlength': '240', # 'name': 'q', 'placeholder': 'Поиск...', 'type': 'text', 'value': ''} driver.quit()