Selenium:元素点击拦截:元素在点(774,8907)处不可单击

fbcarpbf  于 2022-11-24  发布在  其他
关注(0)|答案(2)|浏览(183)

我是 selenium 的新手,但我收到以下错误:element click intercepted: Element is not clickable at point (774, 8907)每当我在有显示更多按钮的网页上运行这段代码时。我的目标是获取网页上“table”的每个元素,但为了做到这一点,我需要单击“show more”按钮(如果它存在的话):

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

我试过使用javascript executor,等到按钮可以点击,然后使用actions滚动到按钮,但这根本不起作用。
示例网站:https://www.privateschoolreview.com/sat-score-stats/california

的所有数据

jvlzgdj9

jvlzgdj91#

因为JavaScript交互.所以必须点击使用JS执行。

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

更新日期:

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)

输出:

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]
lf5gs5x2

lf5gs5x22#

试试这个,它对我很有效:

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()

相关问题