selenium.common.exceptions.NoSuchElementException 使用 Selenium Python 从#shadow-root (open) 中提取元素文本时出错

Posted

技术标签:

【中文标题】selenium.common.exceptions.NoSuchElementException 使用 Selenium Python 从#shadow-root (open) 中提取元素文本时出错【英文标题】:selenium.common.exceptions.NoSuchElementException error extracting element text from #shadow-root (open) using Selenium Python 【发布时间】:2022-01-08 16:51:48 【问题描述】:

我目前正在使用 selenium 从 Sneider Electric 获取产品信息,这是我目前收到的错误:

selenium.common.exceptions.NoSuchElementException: Message:
no such element: Unable to locate element:
"method":"xpath","selector":"/html/body/div[2]/main/div[5]/ul/li/div/div/div/div/div/ul/li[1]/div/div/div[2]/div[2]/section/div/product-cards-wrapper//div/ul/li[1]/product-card/article/div/div[1]/product-card-main-info//div/pes-router-link[2]/a/h3"

目前,我试图从中提取此信息的网站是此 URL: https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs%2B/?N=4176697776&No=0&Nrpp=12

Xpath 文件用于描述他们的产品,根据我的检查和发现是这样的:

/html/body/div[2]/main/div[5]/ul/li/div/div/div/div/div/ul/li[1]/div/div/div[2]/div[2]/section/div/product-cards-wrapper//div/ul/li[1]/product-card//article/div/div[1]/product-card-main-info//div/pes-router-link[2]/a/h3

有什么想法吗??

当前代码:

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//article/div/div[1]/product-card-main-info//div/pes-router-link[2]/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 = "/html/body/div[2]/main/div[5]/ul/li/div/div/div/div/div/ul/li[1]/div/div/div[2]/div[2]/section/div/product-cards-wrapper//div/ul/li["
        xpath += str(y)
        xpath += des
        driver.implicitly_wait(5)
        description.append(driver.find_element(By.XPATH, xpath))
        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)

【问题讨论】:

你能分享你的代码吗? 在 xpath 中使用 classes 和 Id - 删除所有 div,当页面添加一些元素时可以更改。 如果product-card-main-info 是一个类,那么它应该是*[@class="product-card-main-info"] 可能需要更长的时间sleep 这样 javascript 才有时间向 HTML 添加元素 【参考方案1】:

产品描述在#shadow-root (open)


解决方案

要提取所需的文本,您需要使用shadowRoot.querySelector(),您可以使用以下Locator Strategy:

driver.get("https://www.se.com/us/en/product-range/63426-powerlogic-accusine-pcs%2B/?N=4176697776&No=0&Nrpp=12")
time.sleep(5)
description = driver.execute_script('''return document.querySelector("product-cards-wrapper.hydrated").shadowRoot.querySelector("product-card.hydrated").shadowRoot.querySelector("product-card-main-info.hydrated").shadowRoot.querySelector("pes-router-link.description.hydrated a > h3")''')
print(description.text)

控制台输出:

Active harmonic filter - 60 A 380..480 V AC - IP00 enclosure
        

参考文献

您可以在以下位置找到一些相关的详细讨论:

How to locate the First name field within shadow-root (open) within the website https://www.virustotal.com using Selenium and Python How to get past a cookie agreement page using Python and Selenium? Unable to locate the Sign In element within #shadow-root (open) using Selenium and Python

【讨论】:

完美这最终工作但有没有办法让网页上的所有其他项目循环? @JorgeJurado-Garcia 我要研究一下你的具体用例。随时根据您的新要求提出新问题。

以上是关于selenium.common.exceptions.NoSuchElementException 使用 Selenium Python 从#shadow-root (open) 中提取元素文本时出错的主要内容,如果未能解决你的问题,请参考以下文章