盡管我嘗試了除塊,但我收到了 Selenium 例外NoSuchElementException 。這是代碼的簡化版本,堆疊跟蹤表明導致例外的是 WebDriverWait 行:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import (TimeoutException,NoSuchElementException)
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://example.com")
all_rows = []
for row in all_rows:
row.find_element(By.XPATH, './/td[3]').click()
try:
WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, "//button[text() = 'Close']")))
# wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text() = 'Close']")))
except Exception as e:
print(e)
在“斷點”下的除錯選項中,未勾選“引發例外”。
uj5u.com熱心網友回復:
根據此處提供的代碼,例外可能是由row.find_element(By.XPATH, './/td[3]').click()代碼行引起的。
這條線現在不在try塊內。
如果是這樣,您可以簡單地將其移動到try塊中,如下所示:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import (TimeoutException,NoSuchElementException)
driver = webdriver.Chrome('chromedriver.exe')
driver.get("https://example.com")
all_rows = []
for row in all_rows:
try:
row.find_element(By.XPATH, './/td[3]').click()
WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.XPATH, "//button[text() = 'Close']")))
# wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text() = 'Close']")))
except Exception as e:
print(e)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/521048.html
