Home > Mobile >  Python selenium stop loading page once element is found
Python selenium stop loading page once element is found

Time:08-08

I'm scraping a website for dividend information, and the selenium query is taking a very long time because it gets stuck on driver.get(). I tried something like this:

URL = 'https://www.nasdaq.com/market-activity/stocks/aapl/dividend-history'

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import selenium as se

options = se.webdriver.FirefoxOptions()
driver = se.webdriver.Firefox()
driver.get(URL)
WebDriverWait(driver, 10).until(
    EC.presence_of_element_located(
        (By.XPATH, '//div')))
driver.execute_script("window.stop();")

but that doesn't work because WebDriverWait(driver, 10) doesn't run until after driver.get(URL), so it just hangs on driver.get(URL). I also tried:

from selenium.common.exceptions import TimeoutException
driver.set_page_load_timeout(10)
try:
    driver.get(URL)
except TimeoutException:
    driver.execute_script("window.stop();")

This at least limits the query to 10 seconds, because the element I need is always loaded in the first 10 seconds, but I'd rather not have to wait 10 seconds. Is there a way to interrupt the driver.get(URL) when the appropriate element is loaded on the page?

CodePudding user response:

There is a less complex way of obtaining that data, without the overheads of Selenium setup. The data in that dividends table is loaded dynamically, from an api (you can see it in Dev tools - Network tab). The following code will get you that data:

import requests
import pandas as pd

headers= {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36'}

url = 'https://api.nasdaq.com/api/quote/AAPL/dividends?assetclass=stocks'
r = requests.get(url, headers=headers)
df = pd.DataFrame(r.json()['data']['dividends']['rows'])
print(df)

This prints out in the terminal:

exOrEffDate type amount declarationDate recordDate paymentDate
0 08/05/2022 CASH $0.23 07/28/2022 08/08/2022 08/11/2022
1 05/06/2022 CASH $0.23 04/28/2022 05/09/2022 05/12/2022
2 02/04/2022 CASH $0.22 01/27/2022 02/07/2022 02/10/2022
3 11/05/2021 CASH $0.22 10/28/2021 11/08/2021 11/11/2021
4 08/06/2021 CASH $0.22 07/27/2021 08/09/2021 08/12/2021
5 05/07/2021 CASH $0.22 04/28/2021 05/10/2021 05/13/2021
6 02/05/2021 CASH $0.205 01/27/2021 02/08/2021 02/11/2021
7 11/06/2020 CASH $0.205 10/29/2020 11/09/2020 11/12/2020
8 08/07/2020 CASH $0.82 07/30/2020 08/10/2020 08/13/2020
9 05/08/2020 CASH $0.82 04/30/2020 05/11/2020 05/14/2020
10 02/07/2020 CASH $0.77 01/28/2020 02/10/2020 02/13/2020
11 11/07/2019 CASH $0.77 10/30/2019 11/11/2019 11/14/2019
12 08/09/2019 CASH $0.77 07/30/2019 08/12/2019 08/15/2019
13 05/10/2019 CASH $0.77 04/30/2019 05/13/2019 05/16/2019
14 02/08/2019 CASH $0.73 01/29/2019 02/11/2019 02/14/2019
15 11/08/2018 CASH $0.73 11/01/2018 11/12/2018 11/15/2018
16 08/10/2018 CASH $0.73 07/31/2018 08/13/2018 08/16/2018
17 05/11/2018 CASH $0.73 05/01/2018 05/14/2018 05/17/2018
18 02/09/2018 CASH $0.63 02/01/2018 02/12/2018 02/15/2018
19 11/10/2017 CASH $0.63 11/02/2017 11/13/2017 11/16/2017
20 08/10/2017 CASH $0.63 08/01/2017 08/14/2017 08/17/2017
21 05/11/2017 CASH $0.63 05/02/2017 05/15/2017 05/18/2017
22 02/09/2017 CASH $0.57 01/31/2017 02/13/2017 02/16/2017
23 11/03/2016 CASH $0.57 10/25/2016 11/07/2016 11/10/2016
24 08/04/2016 CASH $0.57 07/26/2016 08/08/2016 08/11/2016
25 05/05/2016 CASH $0.57 04/26/2016 05/09/2016 05/12/2016
26 02/04/2016 CASH $0.52 01/26/2016 02/08/2016 02/11/2016
27 11/05/2015 CASH $0.52 10/27/2015 11/09/2015 11/12/2015
28 08/06/2015 CASH $0.52 07/21/2015 08/10/2015 08/13/2015
29 05/07/2015 CASH $0.52 04/27/2015 05/11/2015 05/14/2015
30 02/05/2015 CASH $0.47 01/27/2015 02/09/2015 02/12/2015
31 11/06/2014 CASH $0.47 10/20/2014 11/10/2014 11/13/2014
32 08/07/2014 CASH $0.47 07/22/2014 08/11/2014 08/14/2014
33 05/08/2014 CASH $3.29 04/23/2014 05/12/2014 05/15/2014
34 02/06/2014 CASH $3.05 01/27/2014 02/10/2014 02/13/2014
35 11/06/2013 CASH $3.05 10/28/2013 11/11/2013 11/14/2013
36 08/08/2013 CASH $3.05 07/23/2013 08/12/2013 08/15/2013
37 05/09/2013 CASH $3.05 04/23/2013 05/13/2013 05/16/2013
38 02/07/2013 CASH $2.65 01/23/2013 02/11/2013 02/14/2013
39 11/07/2012 CASH $2.65 10/25/2012 11/12/2012 11/15/2012
40 08/09/2012 CASH $2.65 07/24/2012 08/13/2012 08/16/2012
41 11/21/1995 CASH $0.12 N/A 11/21/1995 N/A
42 08/16/1995 CASH $0.12 N/A 08/16/1995 N/A
43 05/26/1995 CASH $0.12 N/A 05/26/1995 N/A
44 02/13/1995 CASH $0.12 N/A 02/13/1995 N/A
45 11/18/1994 CASH $0.12 N/A 11/18/1994 N/A
46 08/15/1994 CASH $0.12 N/A 08/15/1994 N/A
47 05/27/1994 CASH $0.12 N/A 05/27/1994 N/A
48 02/07/1994 CASH $0.12 N/A 02/07/1994 N/A
49 11/19/1993 CASH $0.12 N/A 11/19/1993 N/A
50 08/16/1993 CASH $0.12 N/A 08/16/1993 N/A
51 05/28/1993 CASH $0.12 N/A 05/28/1993 N/A
52 02/12/1993 CASH $0.12 N/A 02/12/1993 N/A
53 11/30/1992 CASH $0.12 N/A 11/30/1992 N/A
54 08/17/1992 CASH $0.12 N/A 08/17/1992 N/A
55 06/01/1992 CASH $0.12 N/A 06/01/1992 N/A
56 02/14/1992 CASH $0.12 N/A 02/14/1992 N/A
57 11/18/1991 CASH $0.12 N/A 11/18/1991 N/A
58 08/19/1991 CASH $0.12 N/A 08/19/1991 N/A
59 05/20/1991 CASH $0.12 N/A 05/20/1991 N/A
60 02/15/1991 CASH $0.12 N/A 02/15/1991 N/A
61 11/16/1990 CASH $0.12 N/A 11/16/1990 N/A
62 08/20/1990 CASH $0.11 N/A 08/20/1990 N/A
63 05/21/1990 CASH $0.11 N/A 05/21/1990 N/A
64 02/16/1990 CASH $0.11 N/A 02/16/1990 N/A
65 11/17/1989 CASH $0.11 N/A 11/17/1989 N/A
66 08/21/1989 CASH $0.10 N/A 08/21/1989 N/A
67 05/22/1989 CASH $0.10 N/A 05/22/1989 N/A
68 02/17/1989 CASH $0.10 N/A 02/17/1989 N/A
69 11/21/1988 CASH $0.10 N/A 11/21/1988 N/A

  • Related