提问人:joker line 提问时间:11/17/2023 最后编辑:joker line 更新时间:11/17/2023 访问量:27
如何解决 selenium python 中的 raise TimeoutException(message, screen, stacktrace)
How to solve raise TimeoutException(message, screen, stacktrace) in selenium python
问:
所以,我一直在做我的第一个硒项目。这是我想 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”的可点击元素进行交互并废弃表的数据。
答:
0赞
Shawn
11/17/2023
#1
问题的根本原因:如果您注意到,当您单击按钮时,新页面会加载到新选项卡中。在这种情况下,selenium 仍将尝试在当前选项卡(而不是新选项卡)中找到所需的元素。submit
溶液:单击 后,您需要将上下文切换到新选项卡,然后在新选项卡上执行所需的操作。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()
附言:使用这两个变量 和 ,根据您在相应选项卡上的预期操作进行切换。window_before
window_after
评论
0赞
joker line
11/20/2023
嗨,Shawn,非常感谢您在解决我的编程代码中的错误问题方面提供的宝贵帮助。我真的很感谢你的时间和帮助!这是非常有帮助的。
0赞
Shawn
11/20/2023
@jokerline - 很高兴它有帮助。当有人回答我的问题时,我该怎么办?
0赞
joker line
11/27/2023
我已经投票了,但有一个通知“感谢您的反馈!你至少需要 15 个声望才能投票,但你的反馈已被记录下来。
评论