-2

I am new to selenium, and I get the following error: element click intercepted: Element is not clickable at point (774, 8907) whenever I run this code on the webpage that has the show more button. My goal is to get every element of the "table" on the webpage, but in order to do so I need to click "show more" button if it is present:

driver = webdriver.Chrome(options=chrome_options)
driver.maximize_window()
for el in states_pages:
    driver.get(el)
    err = False
    i = 0
    while not err:
        try:
            more_button = driver.find_element(by=By.CLASS_NAME, value='tpl-showmore-content')
            more_button.click()
        except selexp.NoSuchElementException as e:
            err = True
            print(e)
        except selexp.ElementClickInterceptedException as e:
            err = True
            print(e)
        i+=1

I have tried using javascript executor, waiting until the button is clickable and crolling to the button by using actions, but this didn't work at all.

Sample website: https://www.privateschoolreview.com/sat-score-stats/california enter image description here

2 Answers2

2

Because JavaScript interaction. So you have to click using JS execution.

    import time
    while not err:
        try:
            more_button = driver.find_element(by=By.CLASS_NAME, value='tpl-showmore-content')
            driver.execute_script("arguments[0].click();" ,more_button)
            time.sleep(1)
        except selexp.NoSuchElementException as e:
            err = True
            print(e)
        except selexp.ElementClickInterceptedException as e:
            err = True
            print(e)
            break

Update:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from bs4 import BeautifulSoup
import time
import pandas as pd

options = webdriver.ChromeOptions()
options.add_argument("--no-sandbox")
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument("start-maximized")
#options.add_experimental_option("detach", True)


s=Service('./chromedriver')
driver= webdriver.Chrome(service=s, options=options)
url='https://www.privateschoolreview.com/sat-score-stats/california'
driver.get(url)
time.sleep(3)

data =[]
for x in range(4):
    try:
        soup = BeautifulSoup(driver.page_source, 'lxml')
        cards = soup.select('[class="tp-list-row list-row-border-2 bg_hover_change"]')
        print(len(cards))
        for x in cards:
            title = x.select_one('a[class="tpl-school-link top-school"]')
            title = title.get_text(strip=True) if title else 'None'
            data.append(title)

            
        loadMoreButton = driver.find_element(By.CSS_SELECTOR, ".tpl-showmore-content")
            
        if loadMoreButton:
            driver.execute_script("arguments[0].click();" ,loadMoreButton)
            time.sleep(1)

       
    except Exception as e:
        pass
        #print(e)
        break

df= pd.DataFrame(set(data))
print(df)

Output:

                             0
0        St. Lucys Priory High School
1          Glendale Adventist Academy
2                    The Webb Schools
3            Desert Christian Academy
4                New Covenant Academy
..                                ...
113               Renaissance Academy
114                  Oak Grove School
115             Francis Parker School
116  Rolling Hills Preparatory School
117     Lake Tahoe Preparatory School

[118 rows x 1 columns]
Md. Fazlul Hoque
  • 15,806
  • 5
  • 12
  • 32
  • Thx, your solution really helps to get rid of this error, but now I face another problem: the code can't find a showmore button anymore: ``` Message: no such element: Unable to locate element: {"method":"css selector","selector":".tpl-showmore-content"} ``` – njhkugk6i76g6gi6gi7g6 Nov 19 '22 at 15:17
  • .. why Selenium? I would go here for a Requests based solution, scraping that AJAX API, and then using BeautifulSoup to parse the json response. – Barry the Platipus Nov 19 '22 at 15:30
  • Thanks, this (the code after update) worked out! – njhkugk6i76g6gi6gi7g6 Nov 20 '22 at 03:35
0

Try this, it works for me:

show_more_lnk = driver.find_element(By.CSS_SELECTOR, ".tpl-showmore-content")
driver.execute_script("arguments[0].scrollIntoView(true)", show_more_lnk)
time.sleep(2)
show_more_lnk.click()
AbiSaran
  • 2,538
  • 3
  • 9
  • 15
  • I get the following error: ```--------------------------------------------------------------------------- StaleElementReferenceException Traceback (most recent call last) /tmp/ipykernel_27/728423907.py in 12 driver.execute_script("arguments[0].scrollIntoView(true)", show_more_lnk) 13 time.sleep(2) ---> 14 show_more_lnk.click() ``` – njhkugk6i76g6gi6gi7g6 Nov 19 '22 at 15:00
  • It works perfectly fine for me, are you refreshing the page? – AbiSaran Nov 19 '22 at 15:14