Вам нужно будет сгенерировать все возможные перестановки, выбрать их на странице и получить цену за каждую.
Вот что сработало для сайта с двумя выпадающими списками:
from selenium.webdriver.support.select import Select
from selenium.webdriver import Chrome
from selenium.common.exceptions import NoSuchElementException
import itertools
from pprint import pformat
def apply_values(dropdowns, values):
"""
:param dropdowns: list of select DropDown
:param values: list of values to set
:return: dict with key=dropdownName, value=dropDownValue
"""
r = {}
for value in values:
# For each value, get the associated DropDown and set it
for dropdown in dropdowns:
if value in dropdown.values:
try:
dropdown.select_by_visible_text(value)
r[dropdown.name] = value
except NoSuchElementException:
# print("Unable to set the following values {}..Skipping".format(values))
# This is caused by an option not being available with other selected dropdown values
# You can also check the attribute disabled for this
return False
break
return r
driver = Chrome()
driver.get('yourUrl')
els = driver.find_elements_by_css_selector(".msku-sel")
selects = []
for el in els:
# Adding the dropdown name to the select object
name = el.get_attribute('name')
select = Select(el)
select.name = name
selects.append(select)
print("Get all options for each dropdown")
for idx, sel in enumerate(selects):
sel.values = [option.text for option in sel.options][1:]
print("Get all possible permutations")
permutations = list(itertools.product(*[sel.values for sel in selects]))
# Iteration for all possible permutation
print("Select all possible permutation and get price for each")
results = []
for permutation in permutations:
# Resetting all parameter to default
for sel in selects:
sel.select_by_index(0)
# Iteration to set each dropdown
result = apply_values(selects, permutation)
if result:
# Once all dropdown value are set, get the finally price
result['Price'] = driver.find_element_by_id("prcIsum").text
results.append(result)
print(pformat(results))
driver.close()
Результат:
Get all options for each dropdown
Get all possible permutations
Select all possible permutation and get price for each
[{'Colour': 'White', 'Price': '£6.99', 'Size': '6'},
{'Colour': 'White', 'Price': '£6.99', 'Size': '8'},
{'Colour': 'White', 'Price': '£6.99', 'Size': '10'},
{'Colour': 'White', 'Price': '£6.99', 'Size': '12'},
{'Colour': 'White', 'Price': '£6.99', 'Size': '14'},
{'Colour': 'White', 'Price': '£6.99', 'Size': '16'},
{'Colour': 'White', 'Price': '£6.99', 'Size': 'S'},
{'Colour': 'White', 'Price': '£6.99', 'Size': 'M'},
{'Colour': 'White', 'Price': '£6.99', 'Size': 'L'},
{'Colour': 'White', 'Price': '£6.99', 'Size': 'XL'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '6'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '8'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '10'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '12'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '14'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': '16'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': 'S'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': 'M'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': 'L'},
{'Colour': 'Blue', 'Price': '£6.99', 'Size': 'XL'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '6'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '8'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '10'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '12'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '14'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': '16'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': 'S'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': 'M'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': 'L'},
{'Colour': 'Light Green', 'Price': '£6.99', 'Size': 'XL'}]
Я использовал pformat, чтобы напечатать dict результата, но вы можете легко переформатировать для желаемого результата.
Я прокомментировал внутри кода, но если вам нужны пояснения, вы можете спросить