如何解决 selenium python 中引发 TimeoutException(message, screen, stacktrace)

问题描述 投票:0回答:1

所以,我一直在致力于我的第一个硒项目。这是我要废弃的网站https://sekolah.data.kemdikbud.go.id/index.php/Chome/profil/8F9F7295-595A-4C42-A4AA-0009B36BCBE1 当你向下滚动时,有一个可点击的元素“Siswa”。当它点击时,它会显示一些表格。 我的问题是,当我尝试与该元素交互时总是出错。我之前已经确保我有一个正确的 XPATH。我也尝试更改Xpath和定位器,但仍然出错。 这是我的代码

import openpyxl
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.keys import Keys
import pyautogui as py
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

url = 'https://sekolah.data.kemdikbud.go.id/'
chrome_options = Options()
chrome_options.add_experimental_option("detach",True)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()),
                          options=chrome_options)
driver.get(url)

# choose kabupaten/kota
x_element = driver.find_element(By.ID, 'kode_kabupaten')
x = Select(x_element)
x.select_by_visible_text("Prov. Jawa Timur - Kota Surabaya")

# choose jenjang
y_element = driver.find_element(By.ID, 'bentuk_pendidikan')
y = Select(y_element)
y.select_by_visible_text("SMA")

submit = driver.find_element(By.XPATH, '//button[text()="Cari Sekolah"]')
submit.click()

sch_name = driver.find_element(By.XPATH, '//*[@class="text-info"]/b')
sch_name.click()

siswa_link = WebDriverWait(driver, 10).until(
    EC.presence_of_element_located((By.XPATH, '//a[contains(@href, "#pd")]')))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(@href, "#pd")]'))).click()

这是显示的错误消息:

Traceback (most recent call last):
  File "C:\Users\user\PycharmProjects\dikbud\main.py", line 37, in <module>
    EC.presence_of_element_located((By.XPATH, '//a[contains(@href, "#pd")]')))
  File "C:\Users\user\PycharmProjects\dikbud\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
    GetHandleVerifier [0x006A72A3+45731]
    (No symbol) [0x00632D51]
    (No symbol) [0x0052880D]
    (No symbol) [0x0055B940]
    (No symbol) [0x0055BE0B]
    (No symbol) [0x0058D1F2]
    (No symbol) [0x00578024]
    (No symbol) [0x0058B7A2]
    (No symbol) [0x00577DD6]
    (No symbol) [0x005531F6]
    (No symbol) [0x0055439D]
    GetHandleVerifier [0x009B0716+3229462]
    GetHandleVerifier [0x009F84C8+3523784]
    GetHandleVerifier [0x009F214C+3498316]
    GetHandleVerifier [0x00731680+611968]
    (No symbol) [0x0063CCCC]
    (No symbol) [0x00638DF8]
    (No symbol) [0x00638F1D]
    (No symbol) [0x0062B2C7]
    BaseThreadInitThunk [0x757A7BA9+25]
    RtlInitializeExceptionChain [0x778BBD2B+107]
    RtlClearBits [0x778BBCAF+191]

帮助我解决这个问题,这样我就可以与名为“Siswa”的可点击元素进行交互并废弃表的数据。

python selenium-webdriver timeoutexception
1个回答
0
投票

问题的根本原因:如果您注意到,当您单击

submit
按钮时,新页面会在新选项卡中加载。在这种情况下,selenium 仍会尝试在当前选项卡(而不是新选项卡)中找到所需的元素。

解决方案: 单击

submit
后,您需要将上下文切换到新选项卡,然后在新选项卡上执行所需的操作。

代码:参考下面的代码在python selenium中将上下文切换到新选项卡。

submit = driver.find_element(By.XPATH, '//button[text()="Cari Sekolah"]')
submit.click()

# Below line will store the current window in a variable
window_before = driver.window_handles[0]

sch_name = driver.find_element(By.XPATH, '//*[@class="text-info"]/b')
sch_name.click()

# Below line will store the new tab in a variable
window_after = driver.window_handles[1]
# Below line will switch to new tab
driver.switch_to.window(window_after)

siswa_link = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[contains(@href, "#pd")]')))
siswa_link.click()
© www.soinside.com 2019 - 2024. All rights reserved.