簡體   English   中英

如何在不關閉和打開每個元素的瀏覽器的情況下單擊並打開 python selenium 元素中的對象集合

[英]How do I click and open a collection of objects in an element in python selenium without closing and opening the browser for each element

假設我有一個想要抓取的 eccormece 網站,並且我對十大熱門產品感興趣,當深入研究 html 元素時,它是這樣的:

<div>
    <div>
        <span>
            <a href='www.mysite/products/1'>
                Product 1
            </a>
        </spa>
    </div>
    <div>
        <span>
            <a href='www.mysite/products/2'>
                Product 2
            </a>
        </spa>
    </div>
    <div>
        <span>
            <a href='www.mysite/products/3'>
                Product 3
            </a>
        </spa>
    </div>
    <div>
        <span>
            <a href='www.mysite/products/4'>
                Product 4
            </a>
        </spa>
    </div>

</div>

我的第一個解決方案是提取 href 屬性,然后將它們存儲在一個列表中,然后我會為每個屬性打開瀏覽器實例,但是這是有代價的,因為我必須關閉並打開瀏覽器,每次打開它我必須進行身份驗證。 然后我嘗試了解決方案 2。在我的解決方案 2 中,外部 div 是父級,根據 selenium 的處理方式,這意味着我存儲的產品如下:


  product_1 = driver.find_element_by_xpath("//div/div[1]")
  product_2 = driver.find_element_by_xpath("//div/div[2]")
  product_3 = driver.find_element_by_xpath("//div/div[3]")
  product_4 = driver.find_element_by_xpath("//div/div[4]")

所以我的目標是搜索產品,在獲得列表目標框的標簽后,單擊它,go 以提取產品的更多詳細信息,然后在不關閉瀏覽器的情況下返回 go,直到我的列表完成,下面是我的解決方案:

 for i in range(10):
    try:
        num = i + 1
        path = f"//div/div[{num}]/span/a"
        poduct_click = driver.find_element_by_xpath(path)
        driver.execute_script("arguments[0].click();", poduct_click)
        scrape_product_detail() #function that scrapes the whole product detail
        driver.execute_script("window.history.go(-1)") # goes backwards to continue looping
    except NoSuchElementException:
        print('Element not found')
    

問題是它適用於第一個產品,它會刮掉所有細節,然后再返回。 盡管返回到產品頁面,但程序未能找到第二個元素以及隨后出現的元素,我無法理解可能是什么問題。 願你幫忙。 謝謝

謝謝@Debenjan,你確實幫了我很多。 你的解決方案就像一個魅力。 對於那些想知道我是怎么做的人來說,下面的代碼是:

        article_elements = self.find_elements_by_class_name("s-card-image")
        collection = []
        for news_box in article_elements:
            # Pulling the hotel name
            slug = news_box.find_element_by_tag_name(
                'a'
            ).get_attribute('href')

            collection.append(
                slug
            )
       for i in range(len(collection)):
            self.execute_script("window.open()")
            self.switch_to.window(self.window_handles[i+1])
            url = collection[i]
            self.get(url)
            print(self.title, url, self.current_url)
      

@AD 非常感謝您的解決方案也有效,我只需要測試並查看最佳策略和 go 是什么。 非常感謝各位

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM