Home > Software engineering >  for looping a JS path Selector using selenium
for looping a JS path Selector using selenium

Time:12-05

Code:

def page_function():
    driver.get('https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs+/?N=4176697776&No=12&Nrpp=12')
    driver.maximize_window()
    # gets the amount of items in the search bar
    print("Number of products:", 69)
    # for loop to read the product name and descriptions

    # product = driver.find_element(By.CSS_SELECTOR, ".search-item")
    # product = product.text
    # print(product)
    pr = "]/product-card//article/div/div[2]/div[1]/pes-product-price/p/span[1]"
    nam = "]/product-card//article/div/div[1]/product-card-main-info//div/pes-router-link[1]/a"
    des = ') > product-card").shadowRoot.querySelector("product-card-main-info.hydrated").shadowRoot.querySelector("pes-router-link.description.hydrated a > h3")'

    # des_path = "#search-items > .search-item .details > a > .row.pt-5.pb-sm-5 > .multilines-3.text-truncate-multilines.xs-single-col-8.col-12 > .font-weight-bold.text-dark"
    follow_loop = range(1, 70)
    for x in follow_loop:
        y = x
        if (x > 61):
            y = x - 60
        elif (x > 49):
            y = x - 48
        elif (x > 37):
            y = x - 36
        elif (x > 25):
            y = x - 24
        elif(x > 13):
            y = x - 12
        else:
            print("")
        if ( ((x % 13) == 0) ):
            driver.delete_all_cookies()
            next_arrow = driver.find_element(By.CLASS_NAME, "page-links__arrow page-links__arrow--next js-page-link js-page-link-next")
            driver.execute_script("arguments[0].click();", next_arrow)

        xpath  = "return document.querySelector("product-cards-wrapper.hydrated").shadowRoot.querySelector("div > ul > li:nth-child(str(y)) > product-card )
        xpath  = str(y)
        xpath  = des
        xpath  = "'"
        driver.implicitly_wait(10)
        description.append(driver.execute_script(xpath))
        print(description[x].text)
        input("continue??")
        xpath2 = xpath.replace(des, '')
        xpath2  = pr
        unit_price.append(driver.find_element(By.XPATH, xpath2).text)
        xpath3 = xpath2.replace(pr, '')
        xpath3  = nam
        name.append(driver.find_element(By.XPATH, xpath3).text)

ERROR:

File "C:\Users\....\Documents\web_scrap\main.py", line 65, in page_function
    xpath  = 'return document.querySelector("product-cards-wrapper.hydrated").shadowRoot.querySelector("div > ul > li:nth-child('
UnboundLocalError: local variable 'xpath' referenced before assignment

Webpage: Website

Issue:

Currently, using the help of others in stack overflow was able to identify that the description, prices, and product name on the website of their product from shadow root. Therefore, I needed to use the execute_script() command in order to work. Furthermore, I was able to see that each element(s) are just in a different dif class which is what I was trying to do in this code. But however, this doesn't seem to work. Any Ideas??

Thanks

CodePudding user response:

xpath  = "return document ...

is the same as

xpath = xpath   "return document

But you've not defined it yet that's the reason you get the error 'xpath' referenced before assignment, you probably meant to have only:

xpath = "return document.querySelector('product-cards-wrapper.hydrated').shadowRoot.querySelector('div > ul > li:nth-child(str(y)) > product-card')"
  • Related