Selenium停止工作,收到的错误:消息:元素点击拦截:元素在点处不可单击

kuarbcqp  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(244)

我的python脚本浏览一个网站在无头模式下使用 selenium 工作了很长一段时间,但它突然停止工作。'提交'按钮在[此][1]页面是可以轻松点击,但现在不是了。
这是我的代码的一部分,它不再工作:

def result(update: Update, context: CallbackContext):
        global stop_alert, chat_id
        upd = bot.get_updates()
        chat_id = upd[0].message.from_user.id
        try:
            if platform.startswith('win'):
                ser = Service("C:\\Program Files (x86)\\chromedriver.exe")
                op = webdriver.ChromeOptions()
                op.headless = True
                driver = webdriver.Chrome(service=ser, options=op)

            else:
                ser = Service("/usr/local/bin/chromedriver")
                op = webdriver.ChromeOptions()
                op.headless = True
                op.add_argument('--no-sandbox')
                driver = webdriver.Chrome(service=ser, options=op)

        except Exception as e:
            update.message.reply_text(f'<b>Exception occurred in result function\n'
                                      f'while creating driver object</b>\n'
                                      f' {str(e)}', parse_mode='HTML')
            return

        reply = f"\U00002764<b> Welcome to Results on BSE Bot </b>\U00002764\n" \
                f"&#x1F575; {len(companies)} company(s) to scan &#x1F575;"
        update.message.reply_text(reply, parse_mode='HTML')
        channel_msg_id = ''
        wait = WebDriverWait(driver, 10)
        while True:
            if not stop_alert:
                if companies:
                    for i in list(companies):
                        try:
                            driver.get(i)
                            wait.until(
                                EC.presence_of_element_located((By.CSS_SELECTOR, "div.whitebox img[src*='loader']")))
                            wait.until_not(
                                EC.presence_of_element_located((By.CSS_SELECTOR, "div.whitebox img[src*='loader']")))
                            select = Select(driver.find_element(By.ID, 'ddlPeriod'))
                            select.select_by_value('Result')
                            driver.find_element(By.XPATH, '//input[@value="Submit"]').click()
                            table_data = WebDriverWait(driver, 30).until(
                                EC.presence_of_element_located((By.XPATH, "//td[@id='lblann']")))
                        except Exception as e:
                            reply = f"\U00002764 Exception occurred. {str(e)} \U00002764"
                            update.message.reply_text(reply, parse_mode='HTML')
                            companies.remove(i)
                            continue
                        try:
                            exch_rec_date = table_data.find_element(
                                By.XPATH, '//*[@id="lblann"]/table/tbody/tr[4]/td/table[1]/tbody/tr[2]/td/b[1]'). \
                                get_attribute("innerHTML")
                            exch_rec_date_modified = datetime.strptime(exch_rec_date, ' %d-%m-%Y %H:%M:%S').date()
                            company_name = table_data.find_element(
                                By.XPATH, '//*[@id="lblann"]/table/tbody/tr[1]/td/a').get_attribute("innerHTML")
                            heading = table_data.find_element(
                                By.XPATH, '//*[@id="lblann"]/table/tbody/tr[4]/td/table[1]/tbody/tr[1]/td[1]/a'). \
                                get_attribute("innerHTML")
                            link_pdf = table_data.find_element(By.XPATH, "//a[@class='tablebluelink']").get_attribute(
                                'href')
                            exch_rec_time = table_data.find_element(
                                By.XPATH, '//*[@id="lblann"]/table/tbody/tr[4]/td/table[1]/tbody/tr[2]/td/b[1]'). \
                                get_attribute("innerHTML")

This is the error message:

    Message: element click intercepted: Element is not clickable at point (217, 1879)
      (Session info: headless chrome=107.0.5304.107)
    Stacktrace:
    Backtrace:
        Ordinal0 [0x00471ED3+2236115]
        Ordinal0 [0x004092F1+1807089]

  [1]: https://www.bseindia.com/stock-share-price/tanla-platforms-ltd/tanla/532790/corp-announcements/
pkwftd7m

pkwftd7m1#

您必须等待元素变为可单击状态。
首先,这与您正在单击的提交按钮有关,但我也会在选择Result之前添加此检查,因为我看到当页面打开时,微调器一直显示,直到页面加载。
因此,我会将您的代码更改为如下所示:

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

wait = WebDriverWait(driver, 20)

for i in list(companies):
    try:
        driver.get(i)
        wait.until_not(EC.presence_of_element_located((By.CSS_SELECTOR, "div.whitebox img[src*='loader']")))
        select = Select(driver.find_element(By.ID, 'ddlPeriod'))
        select.select_by_value('Result')
        wait.until(EC.element_to_be_clickable((By.XPATH, '//input[@value="Submit"]'))).click()
        table_data = WebDriverWait(driver, 30).until(
        EC.presence_of_element_located((By.XPATH, "//td[@id='lblann']")))
    except Exception as e:
        reply = f"\U00002764 Exception occurred. {str(e)} \U00002764"
        update.message.reply_text(reply, parse_mode='HTML')
        companies.remove(i)
        continue

相关问题