M
M
Mikhail Mikhail2022-01-24 12:31:03
Python
Mikhail Mikhail, 2022-01-24 12:31:03

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()


There is such a code for parsing each li object in the dropdown menu, when the loop is started for the first time, it parses as it should and then gives the error

"Message: stale element reference: element is not attached to the page document
(Session info: chrome=97.0.4692.71)
"

Please tell me https://decolux.in.ua/p982095659-tkanevyj-rolet-lu...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2022-01-24
@latty337

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()
    ...

In this case, it is assumed that on each such page the list of options is the same. Otherwise, of course, we will get an error when trying to go beyond the list.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question