【问题标题】:Python :selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element:Python:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:
【发布时间】:2021-05-04 12:30:58
【问题描述】:

我正在尝试使用 selenium python 从 Youtube 抓取数据。我正在抓取的数据具有订阅者、位置、加入和视图等字段。尽管给出了正确的 xpath,但我得到了这样的错误

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="subscriber-count"]"}
  (Session info: chrome=90.0.4430.93)

我尝试过的方法是使用 css_selectors、完整的 xpath、id、类名,但它们都没有真正起作用。都返回与上述相同的错误。

这是我在 python 脚本中的编写方式:

        youtube_subscribers = browser.find_element_by_xpath('//*[@id="subscriber-count"]').text
        youtube_location = browser.find_element_by_xpath('//*[@id="details-container"]/table/tbody/tr[2]/td[2]/yt-formatted-string').text
        youtube_joined_on = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[2]/span[2]').text
        youtube_views = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[3]').text

        print('Youtube Subscribers:', youtube_subscribers)
        print('Youtube Location:', youtube_location)
        print('Youtube Joined on:', youtube_joined_on)
        print('Youtube views:', youtube_views)

我正在从这里抓取https://www.youtube.com/c/adidas/about。我到底哪里错了? 请帮忙!

编辑:这是相同的完整代码。

website = ['https://www.pinterest.com/adidas/', 'https://www.pinterest.com/nike/', 'https://www.pinterest.com/puma/']
    options = webdriver.ChromeOptions()
    options.add_argument('start-maximized')
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)

    browser = webdriver.Chrome(ChromeDriverManager().install(), options=options)
    delays = [7, 4, 6, 2, 10, 19]
    delay = np.random.choice(delays)
    for crawler in website:
        browser.get(crawler)
        time.sleep(2)
        time.sleep(delay)
        pinterest_brand_name = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/h1').text
        pinterest_followers = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[1]').text
        pinterest_following = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/div[1]/span[1]').text
        youtube_subscribers = browser.find_element_by_xpath('//*[@id="subscriber-count"]').text
        youtube_location = browser.find_element_by_xpath('//*[@id="details-container"]/table/tbody/tr[2]/td[2]/yt-formatted-string').text
        youtube_joined_on = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[2]/span[2]').text
        youtube_views = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[3]').text

        print('Pinterest Brand Name:', pinterest_brand_name)
        print('Pinterest Followers:', pinterest_followers)
        print('Pinterest Following:', pinterest_following)
        print('Youtube Subscribers:', youtube_subscribers)
        print('Youtube Location:', youtube_location)
        print('Youtube Joined on:', youtube_joined_on)
        print('Youtube views:', youtube_views)

【问题讨论】:

    标签: python selenium selenium-webdriver web-scraping


    【解决方案1】:

    我看不到您的其余代码,但假设所有这些元素都在同一页面上,并且在第一个页面上失败了,您可能只需要在其中添加一个等待命令以使第一个元素可见.

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.by import By
    
    youtube_subscribers = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[@id="subscriber-count"]')))
    

    假设你想要的所有元素都在那个页面上,你应该只需要等待上面那个。不过你可以试试看有什么用

    更新代码

    下面应该可以工作,将循环分成两部分,这样它就不会在 Pinterest 网站上尝试查找 YouTube 网络元素时失败,反之亦然...

    website = ['https://www.pinterest.com/adidas/', 'https://www.pinterest.com/nike/', 'https://www.pinterest.com/puma/',
    'https://www.youtube.com/c/adidas/about']
    
    for crawler in website:
        if "pinterest" in crawler:
            browser.get(crawler)
            sleep(3)
            #time.sleep(delay)
            pinterest_brand_name = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/h1').text
            pinterest_followers = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/span[1]').text
            pinterest_following = browser.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/div[1]/span[1]').text
            print('Pinterest Brand Name:', pinterest_brand_name)
            print('Pinterest Followers:', pinterest_followers)
            print('Pinterest Following:', pinterest_following)
        elif "youtube" in crawler:
            browser.get(crawler)
            sleep(3)
            #time.sleep(delay)
            youtube_subscribers = browser.find_element_by_xpath('//*[@id="subscriber-count"]').text
            youtube_location = browser.find_element_by_xpath('//*[@id="details-container"]/table/tbody/tr[2]/td[2]/yt-formatted-string').text
            youtube_joined_on = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[2]/span[2]').text
            youtube_views = browser.find_element_by_xpath('//*[@id="right-column"]/yt-formatted-string[3]').text
            print('Youtube Subscribers:', youtube_subscribers)
            print('Youtube Location:', youtube_location)
            print('Youtube Joined on:', youtube_joined_on)
            print('Youtube views:', youtube_views)
    

    结果:

    Pinterest Brand Name: adidas
    Pinterest Followers: 623,732
    Pinterest Following: 9
    Pinterest Brand Name: Nike
    Pinterest Followers: 765,138
    Pinterest Following: 3
    Pinterest Brand Name: PUMA
    Pinterest Followers: 88,759
    Pinterest Following: 280
    Youtube Subscribers: 925K subscribers
    Youtube Location: United States
    Youtube Joined on: Oct 29, 2005
    Youtube views: 168,725,975 views
    

    【讨论】:

    • 感谢您的及时回复,但这不起作用,我已经编辑了我的问题并添加了整个代码。请检查。
    • 你的问题没有任何意义......“我从这里刮https://www.youtube.com/c/adidas/about。我到底哪里出错了?”如果是这样,你为什么要循环浏览 Pinterest 网站?我建议您弄清楚您要做什么,正确组织并重新发布,以便其他人可以帮助您
    • 顺便说一句,即使您将 https://www.youtube.com/c/adidas/about 添加到 website 列表中,它也不会起作用。你不能在同一个循环中混合不同的网站。对于 Youtube 网站,Pinterest 定位器将在循环中出错,对于 Pinterest 网站,YouTube 定位器每次都会出错,按照您的布局方式
    • 我从 pinterest 和 youtube 都抓取了,但是既然你说你不能在同一个循环中混合不同的网站..我将完全创建一个新功能然后尝试。谢谢指出
    • 我刚刚更新了我的代码来解释我的意思。我没有添加新功能,只是添加了第二个循环。这在我身上进行了测试,似乎有效。如果您愿意,您可以添加一个新功能,这只是为了让您了解我所指的内容。祝你好运
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多