Not sure what i'm missing here, trying to create a new array with all the possible options of the size and color arrays when both are present. Used this as a guide: https://stackoverflow.com/a/5227021/2964789. The desired output should be: Black-L, Black-M, Black-S, Black-XL, Black-XXL, RedL, Red-M, Red-S, Red-XL, Red-XXL
require "capybara/dsl"
require "spreadsheet"
require "fileutils"
require "open-uri"
include Capybara::DSL
Capybara.run_server = false
Capybara.default_driver = :selenium
Capybara.default_selector = :xpath
Spreadsheet.client_encoding = 'UTF-8'
visit "http://www.example.com/sexy-women-pencil-dress-see-through-mesh-stripes-backless-bodycon-slim-party-clubwear-g0376.html"
if page.has_selector?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']") and page.has_no_xpath?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
optionchoice = page.all("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
options = optionchoice.collect(&:text).join(', ')
elsif page.has_selector?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']") and page.has_no_xpath?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
optionchoice = page.all("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
options = optionchoice.collect(&:text).join(', ')
else page.has_selector?("//dd[1]//select[contains(@name, 'options')]//*[@price='0']") and page.has_selector?("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
colorchoice = page.all("//dd[1]//select[contains(@name, 'options')]//*[@price='0']")
colors = colorchoice.collect(&:text).join(', ')
sizechoice = page.all("//dd[2]//select[contains(@name, 'options')]//*[@price='0']")
sizes = sizechoice.collect(&:text).join(', ')
combinedchoice = [[colors],[sizes]]
options = combinedchoice.each.product(*combinedchoice[1..-1]).map(&:join)
end
puts options
Suppose you have 2 arrays, colors and sizes:
colors = ["Black", "Red"]
sizes = ["L", "M", "S", "XL", "XXL"]
To print array with all products (with slash between):
["Black-L","Black-M","Black-S","Black-XL","Black-XXL","Red-L","Red-M","Red-S","Red-XL","Red-XXL"]
Type:
p colors.product(sizes).map { |x| x.join("-") }