Answer the question
In order to leave comments, you need to log in
How can I simulate a click on each li object in the dropdown?
driver.get(item_href)
e = driver.find_element_by_class_name("b-custom-drop-down")
e.click()
time.sleep(1)
html_list = driver.find_element(By.XPATH,"/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[4]/div/table/tbody/tr[2]/td[1]/div/div/ul")
items = html_list.find_elements_by_css_selector(".b-custom-drop-down__list-item")
for item in items:
time.sleep(5)
item.click()
name = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[1]/h1/span')
code = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[2]/ul/li[2]/span')
price = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[3]/div/div/p/span[1]')
amount = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[2]/ul/li[1]')
cur = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[3]/div/div/p/span[2]')
size = driver.find_element(By.XPATH, '/html/body/div[7]/div/div/div[2]/div[1]/div[1]/div/div[2]/div[4]/div/table/tbody/tr[2]/td[1]/div/div/span')
print(f"Наименование: {name.text} Код:{code.text} Наличие: {amount.text} Стоимость: {price.text} {cur.text} Размер: {size.text}")
e = driver.find_element_by_class_name("b-custom-drop-down")
e.click()
Answer the question
In order to leave comments, you need to log in
The problem is that item belongs to the original driver object (the initial loaded page). But after selecting a product variant (clicking item.click() ), a new page with this variant is loaded. As a result, the driver is loaded with another page. And item still continues to refer to the object from the original one. That is why such an error occurs.
I'm not an expert in selenium, maybe there is a more correct way, but in this case, offhand, I can just look for the element to click each time, sorting through the indices, instead of the item objects.
for idx in range(len(items)):
time.sleep(1)
# заново ищем элемент
item = driver.find_element_by_xpath(f"//li[@class='b-custom-drop-down__list-item'][{idx+1}]")
item.click()
...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question