I am trying to access a product but the xpath is not working at all.
first I do a search for the product and when I want to access the first search item, not ”Los más populares’ and I get Message: no such element: Unable to locate element:
the site is https://inkafarma.pe
and with the search https://inkafarma.pe/buscador?keyword=pampers
I tried with switch_to.frame but I can’t figure out the logic.
at the moment my test code is like this
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.chrome.service import Service
import time
# Set up the service with the path to the driver executable
service = Service(executable_path='C:chromedriver_win32chromedriver.exe')
# Configure Chrome options
options = webdriver.ChromeOptions()
prefs = {"profile.default_content_setting_values.notifications": 2}
options.add_experimental_option("prefs", prefs)
# Initialize the Chrome browser
driver = webdriver.Chrome(service=service, options=options)
# Open the Inkafarma page
driver.get("https://inkafarma.pe/")
time.sleep(5)
click_close_win = driver.find_element(By.XPATH, f'/html/body/div[5]/div[2]/div/mat-dialog-container/fp-home-location-contentful/div/div/div/span')
click_close_win.click()
# Wait for the search bar to be available
search_bar = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[id='ctrl-product-searcher']"))
)
search_bar.send_keys("Pampers")
time.sleep(2)
# Find the <label> element with the search image and click on it
search_button = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "label.main-header-search-label"))
)
search_button.click()
# Wait for the results to fully load
time.sleep(10)
# Find the first item in the list using XPath and click on it
try:
# Scroll down the page using JavaScript
driver.execute_script("window.scrollBy(0, 600);")
time.sleep(10)
# Wait for the iframe to be present and switch context to the iframe
# Switch to the selected iframe
frames = driver.switch_to.frame(3)
print("Switched to frame...")
# Switch context back to the main content
name = driver.find_element(By.XPATH, f'/html/body/fp-root/fp-business/div/div/fp-search-landing/div[2]/fp-search-landing-desktop/div/div[2]/div[2]/fp-filtered-product-list/div/div[2]/fp-product-large/div/fp-link')
name.click()
print("First product found. Clicking on it...", name)
except Exception as e:
print("Could not find or click on the first product. Error:", e)
# Close the browser
driver.switch_to.default_content()
Neider Calderon is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.