簡體   English   中英

無頭模式和無頭模式之間的不同結果 python selenium

[英]Different results between headless mode and no headless mode python selenium

我正在使用 chromedriver、selenium 和 BeatifulSoup 抓取以下 web 頁面:

https://www.rappi.com.co/tiendas/exito-express/s?store_type=express_exito&query=man%C3%AD&search_type=TYPED&origin=general

我使用 selenium 與 web 頁面進行交互,在我確定我已經顯示了整個 web 頁面之后我使用 BeatifulSoup 來定位和提取信息

我使用 BeatifulSoup 尋找這個元素

img = n.find("img", {"class":"ng-lazyloading"})["src"]

當我在有頭(無頭)模式下運行腳本時,我得到了所有 src 屬性,但是當我使用無頭模式運行腳本時,我只得到了最后一個 src。

如何使用無頭模式獲取整個 src 屬性。

這是設置的參數:

options = webdriver.ChromeOptions()
options.add_argument("--incognito")
options.add_argument("--headless")
options.add_argument('--no-sandbox')
options.add_argument('window-size=1051x806')
#options.add_argument("start-maximized")
# options.use_chromium = True
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(executable_path= ChromeDriverManager().install(), options=options)
driver.set_page_load_timeout(30)
driver.implicitly_wait(10)


look at the output

在此處輸入圖像描述

可能您可以嘗試使用 selenium 驅動程序本身來獲取 src 鏈接,

enter code here
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

img=[]

l=WebDriverWait(driver, 60).until(EC.visibility_of_all_elements_located((By.XPATH,"//img[@class='  ng-lazyloaded']")))

for i in l:
    img.append(i.get_attribute('src'))

暫無
暫無

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

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