1

I'm currently teaching myself how to use Python/Selenium.

I have a block of code that lists all available shirts on a webpage then lists all available colors but I want the shirts/colors to show up together instead of 2 separate lists.

Here's what I've got so far:

shirts = driver.find_elements_by_xpath("""//*[@id="container"]/article/div/h1/a""")
for shirt in shirts:
    text = shirt.text
    print text
colors = driver.find_elements_by_xpath("""//*[@id="container"]/article/div/p/a""")
for color in colors:
    text = color.text
    print text

Here's the results of the above code:

Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Zip Up Hooded Sweatshirt
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Contrast Crewneck
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Jet Sleeve Zip Up Hooded Sweatshirt
Navy
Red
Heather Grey
Dark Green
Light Brown
Black
Heather Grey
Light Brown
Black
Dark Green
Red
Navy
Violet
Light Pine
Black
White
Navy

1 Answer 1

1

Use zip method:

shirts = driver.find_elements_by_xpath("//*[@id='container']/article/div/h1/a")
colors = driver.find_elements_by_xpath("//*[@id='container']/article/div/p/a")
for shirt, color in zip(shirts, colors):
    shirt_text = shirt.text
    color_text = color.text
    print shirt_text, color_text
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.