from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://www.amazon.com.au/?&tag=hydramzkw0au-22&ref=pd_sl_6wcb7ezr9q_e&adgrpid=87712394315&hvpone=&hvptwo=&hvadid=414047633191&hvpos=&hvnetw=g&hvrand=17758126363033746916&hvqmt=e&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1000286&hvtargid=kwd-10573980&hydadcr=179_487&gclid=CjwKCAiA8bqOBhANEiwA-sIlNxQufQ7Vywkurf9_zmHedwoBR-vCcDaw7zLcvdHmtp23d7OxJeEgThoCxu8QAvD_BwE")
link = driver.find_element(By.ID, "twotabsearchtextbox")
link.send_keys("Wooden box")
link.send_keys(Keys.RETURN)
try:
main = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "sg-col-inner"))
)
results = main.find_elements(By.CLASS_NAME, "sg-col-inner")
for result in results:
resultname = results.find_element(By.CLASS_NAME, "a-size-base-plus a-color-base a-text-normal")
print(resultname)
except:
driver.quit()
I am having problems with the for loop, I am able to search 'Wooden Box', but I am unable to print the results that I get from searching 'Wooden Box. I am very new to python and any help is appreciated.
CodePudding user response:
To print the results attributes you need to induce WebDriverWait for the visibility_of_all_elements_located() and you can use the following Locator Strategies:
Code Block:
driver.get("https://www.amazon.com.au/?&tag=hydramzkw0au-22&ref=pd_sl_6wcb7ezr9q_e&adgrpid=87712394315&hvpone=&hvptwo=&hvadid=414047633191&hvpos=&hvnetw=g&hvrand=17758126363033746916&hvqmt=e&hvdev=c&hvdvcmdl=&hvlocint=&hvlocphy=1000286&hvtargid=kwd-10573980&hydadcr=179_487&gclid=CjwKCAiA8bqOBhANEiwA-sIlNxQufQ7Vywkurf9_zmHedwoBR-vCcDaw7zLcvdHmtp23d7OxJeEgThoCxu8QAvD_BwE") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#twotabsearchtextbox"))).send_keys("Wooden box" Keys.RETURN) print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='sg-col-inner']//div[@class='a-section a-spacing-small a-spacing-top-small']/span[@dir='auto' and contains(., 'results')]"))).text) print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//span[@class='a-size-base-plus a-color-base a-text-normal']")))])
Console Output:
['Nirvana Class Handmade Wooden Jewellery Trinket Box Keepsake Storage Organizer with Hand Carved Celtic Design', 'Large Wooden Box with Hinged Lid - Wood Storage Box with Lid - Black Stash Box - Wooden Storage Box - Decorative Boxes with lids Stash Boxes (Matte Black)', 'Wooden Box Jewelry Trinket Box Storage Organizer Trinket Storage.', 'TRUST- Wooden Box Keepsake Jewelry Trinket Box Storage Organizer Trinket Storage Keepsake Jewelry', 'Handmade Wooden Jewelry Box/Case / Storage for Women Jewel Organizer Gift Items', '(Dark Brown) - Natural Wood Stash Box with Rolling Tray Lid w/ Extra Large Storage - Dark Brown - Premium Quality Dovetail Design Discrete Wooden Boxes Bamboo Home Decor (Dark Brown)', '3 Piece Wooden Treasure Box - Keepsake Box - Treasure Chest with Flower Motif for Jewelry', 'Unfinished Wooden Box Bundled with 6 Tubes of Paint and 3 Paintbrushes — Stash Box with Hinged Lid and Front Clasps — Decorative Box, Storage Box, Photo Box — DIY Craft — 9.8 x 7.8 x 4.7 Inches', 'Wooden Box with Hinged Lid, EXISTING Wood Storage Box with Lid, Wooden Memory Keepsake Box - Store Jewelry, Keepsakes, Trinkets and Treasure in Decorative Jewelry Box - Small Walnut Wood Box', '1PC Wooden Ring Box Wooden Printed Personalized Rustic Wedding Jewelry Ring Holder Valentines Day Anniversary Engagement Wedding Gift Jewelry Box', 'IdeaWoodCompany Ring Bearer Box - Pillow Alternative - Ring Holder Box - Wooden Wedding mrs mr Ring Box - Double Ring Box for Wedding Ceremony', 'Sumnacon Wooden Rectangular Tissue Box Cover - Multifunctional Tissue Box Holder with Stationery Remote Control Box, Decorative Tissue Pen Remote Organizer for Home/Office/Car/Restaurant (Retro Yellow)', 'ITOS365 Handmade Wooden Keepsake Storage Case Jewelry Box Jewel Organizer - Gifts for Women, 8 x 5 Inches', 'Wooden Stash Box with Rolling Tray Stash Box Combo to Organise your Herbs and Accessories - Rolling Kit with Removable divider - Large stash box and Jewelry box (Bamboo)', 'Antique Style Wood Storage Box - Decorative box for Home or Office - Wooden Box with Hinged Lid -', 'Blood Will (aka The Unwilling)', 'Melissa & Doug MND488 Band-in-a-Box Clap Clang Tap Musical Instruments (Various Instruments, Wooden Storage Crate, 10-Piece Set, 7.62 cm H x 28.702 cm W x 37.846 cm L)', 'Wood Planter Box, 5 Inch Square, Rustic Barn Wood, Plastic Liner, Garden Centerpiece Display, Wedding Flowers Holder, Home and Venue Decor, (Set of 4)', 'Blake & Lake Tree of Life Keepsake Box - Wooden Keepsake Box with Hinged Lid - Engraved Design - Decorative Wooden Boxes with Lid - Wood Gift Box (Tree of Life)', 'BEAUDORA Jewellery Music Box Wooden Pink Princess Dream Castle Storage Organizer Chest Girls Alice Melody', 'Storage Box, Wooden Storage Box 25 Compartment Essential Oil Container, Essential Oil Display Holder 24 Slots for Essential Oil Storage Box Shop Home Use DIY', 'Large Wooden Stash Box Made of Bamboo. With Sliding No Mess Rolling Tray, Brush & 3 Airtight Smell Proof Jars. Perfect Sturdy Container Kit for Storing Your Herbs and Accessories', 'Large Storage Box with Sliding Lid - Antique Box - Large Storage Box with lid and Combo Lock - Wooden Keepsake Box with Lock - Wood Gift Boxes (Box with Extra Tray)', 'Small Wooden Storage Box with Drawers, Desk Organizer, Vintage French Design (9.75 x 7 x 5 In)', 'Small Jewelry Box Decorative Wooden Jewelry Box, Trinket Box, Mini Storage Chest for Jewelry, Memento Case, Wood Holder for Miscellaneous Teabags Coins, Rustic Antique Distressed Container', 'KidKraft 14953 Austin Toy Box Natural', 'Victrola Wooden Record Crate, Wood color', 'Aroma Designs Wooden Box with Labels for Essential Oil Bottles (Holds 25)', 'Great Birthday Gifts Handmade Decorative Wooden Jewelry Box Jewelry Organizer Keepsake Box Treasure Chest Trinket Holder Watch Box Storage Lock Box 8 x 5 Inches Housewarming Gift Ideas', 'YGEOMER 2pcs 42oz Loaf Soap Mold, Rectangular Silicone Mold Set for Making Soap, with Wooden Boxes, 2 Cutters and 100pcs 4x6 inches Bags', 'ROKR 3D Wooden Puzzles Model Building Kits Mechanical Music Box Kits Creative Gift for Kids on Birthday/Christmas Day', 'Lulli Dried Herbs for Witchcraft - 24 Bottles of Magical Herbal Supplies for Pagan, Wiccan, Witch Spells & Ritual - with Lavender, Mugwort, Sage, Thistle - Essential Kit for Altar, Magic (Wooden Box)', 'Large Wooden Box with Hinged Lid - Wood Storage Box with Lid - Wooden Keepsake Box - Decorative boxes with lids (Black)', 'Double Watch Winders for Automatic Watches ,Solid Wooden 2 Watch Winder Box for Automatic Watches with Super Quiet Motor Watch Spinner ,5 Rotation Mode Settings with Blue Led Light, Soft Modern Watch Pillows ,Two Watch Winder Fit Women and Men Gifts', 'Bits and Pieces - The Secret Enigma Gift Box - Wooden Brainteaser Puzzle Box', 'DharmaObjects Hand Carved Tree of Life Wooden Box Keepsake Storage Multi Utility', 'Soul & Lane Miranda Decorative Wooden Storage Boxes (Set of 2) | Wooden Chest Trunk for Keepsake Toys Photos Memories Closet Nursery Office Bedroom Decoration', 'MOROBOR Jewelry Box Antique Lock Latch Hasp Hinges Handle Box Corner Protectors Kit for DIY Jewelry Box Bronze, Antique Brass Wood Case Jewelry Chest Storage Box Feet Leg Corner Protector, Box not Included', 'EZDC Set of 3 Nesting Wooden Crates, 16 x 12” Wall Mounted Wooden Basket, Storage Crates, Wooden Crate Box for Storage, Display, Decoration', 'Wooden Storage box with Compartments and Key Lock - Locking Keepsake Box for Collectibles - Decorative Wood Box with Hinged Lid (Dark Brown)', 'Stonebriar Vintage Worn Blue Floral Wooden Keepsake Box with Hinged Lid, Storage for Trinkets and Memorabilia, Decorative Jewelry Holder, Gift Idea for Birthdays, Christmas, Weddings, or Any Occasion', 'ROKR 3D Wooden Puzzle Orrery Music Box - Mechanical DIY Solar System Kit Musical Hands-on Activity Toys Gifts for Teens Man/Woman Family', 'WaaHome Small Treasure Chest Decorative Wooden Treasure Box', '55-606 30009 Set of 9 Screwdrivers with 9 Tubes with Spare Blades in Wooden Box Watch Repair Kit', 'Wooden Stash Box Large – Handmade Decorative Stash Box - 9” x 9” x 3.5” Storage Box - Premium Quality Dovetail Design Discrete Wood Stash Boxes (large)', 'Wooden Essential Oil Box - Holds 25 Bottles Size 5-15 mL', 'Set of 3 Wooden Treasure Chest Boxes, Vintage Antique Small Decorative Trunks', 'LONG TAO 2 Pcs 7.5" × 5" × 2.4" Wooden Succulent Planter Plant Container Box Rectangular Flower Pot']
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC