【问题标题】:Selenium fails to load elements, despite EC, waits, and scrolling attemptsSelenium 无法加载元素,尽管有 EC、等待和滚动尝试
【发布时间】:2021-05-23 14:47:30
【问题描述】:

使用 Selenium (3.141)、BeautifulSoup (4.7.9) 和 Python (3.79),我试图了解给定电影/节目的流媒体、租赁和购买选项。我花了几个小时试图解决这个问题,所以任何帮助将不胜感激。就在 cmet 中的混合和之前的尝试而言,格式不佳表示歉意。

示例链接:https://www.justwatch.com/us/tv-show/24

Desired Outcome 是一个漂亮的汤元素,然后我可以解析它(例如,哪些流媒体服务拥有它,有多少季节可用等), 它有 3 个元素(截至目前)——Hulu、IMDB TV 和 DirecTV。

我尝试了许多变体,但只获得了示例链接的 3 种流媒体服务之一,即便如此,结果也不一致。通常,我得到一个空对象。
我尝试过的一些事情包括等待预期条件(存在或可见性),显式使用时间库中的 sleep()。我使用的是 Mac(但通过 USB 运行 Linux),所以物理键盘上没有“PAGE DOWN”。对于键模块,我尝试了 control+arrow down、page down 和空格(空格键),但在这个特定的网页上它们不起作用。但是,如果我以正常方式浏览它,控制 + 向下箭头和空格键有助于将所需部分滚动到视图中。据我所知,在 Keys 中没有 fn + 向下箭头选项,但这是我可以以正常方式移动的另一种方式。

我已经运行了 headless 和常规选项来尝试调试,并尝试了 Firefox 和 Chrome 驱动程序。

这是我的代码:

import time
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup

firefox_options = Options()
firefox_options.add_argument('--enable-javascript') # double-checking to make sure that javascript is enabled
firefox_options.add_argument('--headless')
firefox_driver_path = 'geckodriver'
driver = webdriver.Firefox(executable_path=firefox_driver_path, options=firefox_options)

url_link = 'https://www.justwatch.com/us/tv-show/24'
driver.get(url_link) # initial page
cookies = driver.get_cookies()

我围绕这部分代码尝试的示例

各种 time.sleep(3) 和 driver.implicitly_wait(3) 命令 webdriver.ActionChains(driver).key_down(Keys.CONTROL).key_down(Keys.ARROW_DOWN).perform() webdriver.ActionChains(driver).key_down(Keys.SPACE).perform()

此代码在使用时会产生超时错误

stream_results = WebDriverWait(驱动程序, 15) stream_results.until(EC.presence_of_element_located( (By.CLASS_NAME, "price-comparison__grid__row price-comparison__grid__row--stream")))

page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser') # 'lxml' didn't work either

这是获取与流服务相关的 html 的代码。我还尝试在树的各个级别、ID 和类中获取 html 代码,但代码不存在

stream_row = soup.find('div', attrs={'class':'price-comparison__grid__row price-comparison__grid__row--stream'})
stream_row_holder = soup.find('div', attrs={'class':'price-comparison__grid__row__holder'})
stream_items = stream_row_holder\
                    .find_all('div', attrs={'class':'price-comparison__grid__row__element__icon'})
                    
driver.quit()

【问题讨论】:

    标签: selenium selenium-webdriver beautifulsoup


    【解决方案1】:

    我不确定您是说您的代码在某些情况下是否有效,但我使用 chrome 并且最后的四个 find_all() 行都会产生结果。如果这不是你的意思,请告诉我。您可能缺少的一件事是足够长的time.sleep()。这可能是唯一的区别......

    请注意,您需要 chromedriver 来运行此代码,但也许您有 chrome 并且可以下载 chromedriver.exe。

    import time
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    
    url = 'https://www.justwatch.com/us/tv-show/24'
    driver = webdriver.Chrome('chromedriver.exe', options=chrome_options)
    driver.get(url)
    time.sleep(5)
    html = driver.page_source
    soup = BeautifulSoup(html, "html.parser")
    
    soup.find_all(class_="price-comparison__grid__row__price")
    soup.find_all(class_="price-comparison__grid__row__holder")
    soup.find_all(class_="price-comparison__grid__row__element__icon")
    soup.find_all(class_="price-comparison__grid__row--stream")
    

    这是最后一行的输出:

    [<div class="price-comparison__grid__row price-comparison__grid__row--stream"><div class="price-comparison__grid__row__title price-comparison__promoted__title"> Stream </div><div class="price-comparison__grid__row__holder"><!-- --><div class="price-comparison__grid__row__element"><div class="presentation-type price-comparison__grid__row__element__icon"><a href="https://click.justwatch.com/a?r=https%3A%2F%2Fwww.hulu.com%2Fwatch%2F97e1a3fe-f313-44c7-978c-23537db1bab9&amp;cx=eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL3RpdGxlX2NvbnRleHQvanNvbnNjaGVtYS8xLTAtMCIsImRhdGEiOnsidGl0bGVJZCI6NzY4OCwib2JqZWN0VHlwZSI6InNob3ciLCJqd0VudGl0eUlkIjoidHM3Njg4In19LHsic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL2NsaWNrb3V0X2NvbnRleHQvanNvbnNjaGVtYS8xLTItMCIsImRhdGEiOnsicHJvdmlkZXIiOiJIdWx1IiwibW9uZXRpemF0aW9uVHlwZSI6ImZsYXRyYXRlIiwicHJlc2VudGF0aW9uVHlwZSI6ImhkIiwiY3VycmVuY3kiOiJVU0QiLCJwcm92aWRlcklkIjoxNSwicGFydG5lcklkIjoxfX1dfQ&amp;uct_country=US&amp;uct_buybox=special&amp;sid=" target="_blank"><img alt="Hulu" class="jw-provider-icon price-comparison__grid__row__icon" src="https://images.justwatch.com/icon/116305230/s100" title="Hulu"/><div class="price-comparison__grid__row__price"> 9 Seasons <span class="price-comparison__badge price-comparison__badge--hd price-comparison__badge--hd"> HD </span></div></a></div></div><!-- --></div></div>,
     <div class="price-comparison__grid__row price-comparison__grid__row--stream"><div class="price-comparison__grid__row__title"> Stream </div><div class="price-comparison__grid__row__holder"><!-- --><div class="price-comparison__grid__row__element"><div class="presentation-type price-comparison__grid__row__element__icon"><a href="https://click.justwatch.com/a?r=https%3A%2F%2Fwww.hulu.com%2Fwatch%2F97e1a3fe-f313-44c7-978c-23537db1bab9&amp;cx=eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL3RpdGxlX2NvbnRleHQvanNvbnNjaGVtYS8xLTAtMCIsImRhdGEiOnsidGl0bGVJZCI6NzY4OCwib2JqZWN0VHlwZSI6InNob3ciLCJqd0VudGl0eUlkIjoidHM3Njg4In19LHsic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL2NsaWNrb3V0X2NvbnRleHQvanNvbnNjaGVtYS8xLTItMCIsImRhdGEiOnsicHJvdmlkZXIiOiJIdWx1IiwibW9uZXRpemF0aW9uVHlwZSI6ImZsYXRyYXRlIiwicHJlc2VudGF0aW9uVHlwZSI6ImhkIiwiY3VycmVuY3kiOiJVU0QiLCJwcm92aWRlcklkIjoxNSwicGFydG5lcklkIjoxfX1dfQ&amp;uct_country=US&amp;uct_buybox=normal&amp;sid=" target="_blank"><img alt="Hulu" class="jw-provider-icon price-comparison__grid__row__icon" src="https://images.justwatch.com/icon/116305230/s100" title="Hulu"/><div class="price-comparison__grid__row__price"> 9 Seasons <span class="price-comparison__badge price-comparison__badge--hd price-comparison__badge--hd"> HD </span></div></a></div></div><div class="price-comparison__grid__row__element"><div class="presentation-type price-comparison__grid__row__element__icon"><a href="https://click.justwatch.com/a?r=https%3A%2F%2Fwww.imdb.com%2Ftv%2Fwatch%2Ftt0502165&amp;cx=eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL3RpdGxlX2NvbnRleHQvanNvbnNjaGVtYS8xLTAtMCIsImRhdGEiOnsidGl0bGVJZCI6NzY4OCwib2JqZWN0VHlwZSI6InNob3ciLCJqd0VudGl0eUlkIjoidHM3Njg4In19LHsic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL2NsaWNrb3V0X2NvbnRleHQvanNvbnNjaGVtYS8xLTItMCIsImRhdGEiOnsicHJvdmlkZXIiOiJJTURiIFRWIiwibW9uZXRpemF0aW9uVHlwZSI6ImFkcyIsInByZXNlbnRhdGlvblR5cGUiOiJzZCIsImN1cnJlbmN5IjoiVVNEIiwicHJvdmlkZXJJZCI6MjM4LCJwYXJ0bmVySWQiOjF9fV19&amp;uct_country=US&amp;uct_buybox=normal&amp;sid=" target="_blank"><img alt="IMDb TV" class="jw-provider-icon price-comparison__grid__row__icon" src="https://images.justwatch.com/icon/134049674/s100" title="IMDb TV"/><div class="price-comparison__grid__row__price"> 8 Seasons <!-- --></div></a></div></div><div class="price-comparison__grid__row__element"><div class="presentation-type price-comparison__grid__row__element__icon"><a href="https://click.justwatch.com/a?r=https%3A%2F%2Fwww.directv.com%2Ftv%2FThe-Last-24-bGF0SVVYTWtqR1NGby9GckxSaXJvUT09%2FSomebody-Knows-Something-Vm9TYUoxOW1hN1grZFAvTTlPKzdXdz09&amp;cx=eyJzY2hlbWEiOiJpZ2x1OmNvbS5zbm93cGxvd2FuYWx5dGljcy5zbm93cGxvdy9jb250ZXh0cy9qc29uc2NoZW1hLzEtMC0wIiwiZGF0YSI6W3sic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL3RpdGxlX2NvbnRleHQvanNvbnNjaGVtYS8xLTAtMCIsImRhdGEiOnsidGl0bGVJZCI6NzY4OCwib2JqZWN0VHlwZSI6InNob3ciLCJqd0VudGl0eUlkIjoidHM3Njg4In19LHsic2NoZW1hIjoiaWdsdTpjb20uanVzdHdhdGNoL2NsaWNrb3V0X2NvbnRleHQvanNvbnNjaGVtYS8xLTItMCIsImRhdGEiOnsicHJvdmlkZXIiOiJESVJFQ1RWIiwibW9uZXRpemF0aW9uVHlwZSI6ImZsYXRyYXRlIiwicHJlc2VudGF0aW9uVHlwZSI6ImhkIiwiY3VycmVuY3kiOiJVU0QiLCJwcm92aWRlcklkIjozNTgsInBhcnRuZXJJZCI6MX19XX0&amp;uct_country=US&amp;uct_buybox=normal&amp;sid=" target="_blank"><img alt="DIRECTV" class="jw-provider-icon price-comparison__grid__row__icon" src="https://images.justwatch.com/icon/158260222/s100" title="DIRECTV"/><div class="price-comparison__grid__row__price"> 1 Season <span class="price-comparison__badge price-comparison__badge--hd price-comparison__badge--hd"> HD </span></div></a></div></div><!-- --></div></div>]
    

    【讨论】:

    • 非常感谢。是的,我的帖子有点混乱。一些代码可以工作,但不是所有的。感谢您的指导,我得到了它与 chromedriver 的合作。我还想到 NoScript(我在 Firefox 中安装的 javascript 阻止程序)可能与 geckodriver 发生冲突。我的其他废品项目与 geckodriver 没有问题,但我很高兴它现在可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 2019-11-02
    • 2021-07-23
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多