I'm creating a bot that would require Selenium to click on the search bar for Walmart to input the name of a product and then search it. The problem is no matter what I use "
or or ID = "header-input-search"
it doesn't seem to be able to find the search bar to input the product name to search. The error code I get is "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element"
driver.get("https://www.walmart.com/")
time.sleep(4)
searchW = driver.find_element(By.CSS_SELECTOR,".b--none f5 flex-auto lh-solid sans-serif br-pill")
searchW.send_keys(product)
searchW.send_keys(Keys.RETURN)
try:
itemWTitle = driver.find_element(By.CSS_SELECTOR,(".absolute w-100 h-100 z-1 hide-sibling-opacity"))
print(itemWTitle.text)
itemWPrice = driver.find_element(By.CSS_SELECTOR,(".b black f5 mr1 mr2-xl lh-copy f4-l"))
print(itemWPrice.text[0:7])
except:
print("Couldn't find product at Walmart")
CodePudding user response:
Try using xpath and the searching for the name attribute
driver.get("https://www.walmart.com/")
time.sleep(4)
searchW = driver.find_element(By.XPATH, "//input[@name='q']")
searchW.send_keys(product)
searchW.send_keys(Keys.RETURN)
Update
The actual reason why you cannot find it is because walmart is detecting that you are using an automation tool and needlessly forces you to validate a bunch of repetitive captchas and other tests.
CodePudding user response:
This is one way of accessing that page without being blocked (of course, if your IP isn't already blacklisted):
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import time as t
import undetected_chromedriver as uc
options = uc.ChromeOptions()
# options.add_argument("--no-sandbox")
# options.add_argument('--disable-notifications')
options.add_argument("--window-size=1280,720")
browser = uc.Chrome(options=options)
actions = ActionChains(browser)
wait = WebDriverWait(browser, 20)
url = 'https://www.walmart.com/'
browser.get(url)
t.sleep(5)
searchbox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[aria-label="Search"]')))
actions.move_to_element(searchbox).perform()
t.sleep(1)
searchbox.click()
searchbox.send_keys('AR-15')
searchbox.send_keys(Keys.ENTER)
To my pleasant surprise, they stopped selling the real thing. Maybe there is hope after all.. but I digress. For Selenium documentation, you can visit: https://www.selenium.dev/documentation/ And for undetected_chromedriver, see https://pypi.org/project/undetected-chromedriver/