使用 selenium 循环 JS 路径选择器
for looping a JS path Selector using selenium
代码:
def page_function():
driver.get('https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs%2B/?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)
错误:
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
网页:
Website
问题:
目前在stack overflow中借助他人的帮助,能够从shadow root中识别出其产品网站上的描述、价格和产品名称。因此,我需要使用 execute_script() 命令才能工作。此外,我能够看到每个元素都位于不同的 dif class 中,这正是我在此代码中尝试做的。但是,这似乎不起作用。有什么想法吗??
谢谢
xpath += "return document ...
与
相同
xpath = xpath + "return document
但是您还没有定义它,这就是您收到错误 'xpath' referenced before assignment
的原因,您可能只想:
xpath = "return document.querySelector('product-cards-wrapper.hydrated').shadowRoot.querySelector('div > ul > li:nth-child(str(y)) > product-card')"
代码:
def page_function():
driver.get('https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs%2B/?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)
错误:
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
网页: Website
问题:
目前在stack overflow中借助他人的帮助,能够从shadow root中识别出其产品网站上的描述、价格和产品名称。因此,我需要使用 execute_script() 命令才能工作。此外,我能够看到每个元素都位于不同的 dif class 中,这正是我在此代码中尝试做的。但是,这似乎不起作用。有什么想法吗??
谢谢
xpath += "return document ...
与
相同xpath = xpath + "return document
但是您还没有定义它,这就是您收到错误 'xpath' referenced before assignment
的原因,您可能只想:
xpath = "return document.querySelector('product-cards-wrapper.hydrated').shadowRoot.querySelector('div > ul > li:nth-child(str(y)) > product-card')"