You can easily find elements (by css selectors) in this case, using just JSON mapping file.
Clone this repo to your current directory
git clone https://github.com/galihanggara68/selenium-finder.git
or install using pip
pip3 install git+https://github.com/galihanggara68/selenium-finder.git@master
First of all import Finder class
from Selenium_Finder.finder import Finder
Finder
class has 2 parameters driver
the Selenium WebDriver and options
(optional) dictionary where you can customize some options within Finder
class
from selenium import webdriver
driver = webdriver.Chrome()
# global_wait default 10 second
# iterable_each_wait default 1 second
options = {"global_wait": 10, "iterable_each_wait": 2}
finder = Finder(driver, options)
driver.get("https://example.com")
# load mapping data
finder.load_mapping("json_mapping.json")
# execute finder
finder.by_json_scheme()
# get successfully mapped data
print(finder.get_mapped_data())
execute Finder object
load JSON mapping file json_path JSON file path
get current mapping scheme
set mapping scheme as current mapping new_mapping mapping dictionary
get successful mapped data
{
[map_name]: {
"type": ["clickable" | "typable" | "iterable" | "text" | "value" | "attribute"],
"elem": [css selector element]
}
}
{
[map name]: {
"type": ["text" | "value"],
"elem": [css selector element]
}
}
{
[map name]: {
"type": "attribute",
"elem": [css selector element],
"attribute": [attribute name]
}
}
effect
propery will executed after element clicked
{
[map name]: {
"type": "clickable",
"elem": [css selector element],
"effect": {
[another mapping]
}
}
}
text
propery is a keys that sent to element
enter
boolean, if true it will send enter key
{
"search": {
"type": "typable",
"elem": [css selector element],
"text": [text to send],
"enter": [true | false]
},
}
map
will be a mapper for each item iterated
{
[map name]: {
"type": "iterable",
"elem": [css selector element],
"map": {
[map name 1]: [selector 1],
[map name n]: [selector n],
}
}
}
{
"search": {
"type": "typable",
"elem": ".search",
"text": "Query",
"enter": true
},
"user": {
"type": "clickable",
"elem": "a.app-aware-link.artdeco-button[href*=Profile]",
"effect": {
"type": "text",
"elem": "section h1"
}
},
"skills": {
"type": "iterable",
"elem": "#skills ~ div ul > li > div",
"map": {
"name": "div > div span.t-bold",
"level": "div > div span.t-muted"
}
}
}