JH N
JH N

Reputation: 65

how to click on item in UL using pythong selenium

I want to click the 1st item, I tried locating it by using find element xpath, tag name, but im getting an error

Upvotes: 1

Views: 180

Answers (1)

Vova
Vova

Reputation: 3541

first you should realize, that selector what you're trying to interract with the page is not unique and class name is not enough to get the particular one

second I would suggest again using explicitly waits:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


timeout = 30
driver = webdriver.Chrome()
driver.get('https://weed.com/product-category/cbd/')
element = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, "(//a[@class='woocommerce-LoopProduct-link woocommerce-loop-product__link'])[1]")))

element.click()

driver.quit()

if you want to click on the particular element, I would suggest using text and action-perform:

timeout = 30
driver = webdriver.Chrome()
driver.get('https://weed.com/product-category/cbd/')

element = WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.XPATH, "//h2[text()='Binoid Good Night CBD Oil – Sleep Blend']")))

action = ActionChains(driver)

action.move_to_element(element).click().perform()

driver.quit()

Upvotes: 1

Related Questions