selenium - Selenium:try/except - driver.driver.find_element_by_xpath().click - 无法传递“selenium.common.exceptions.NoSuchElementException”
问题描述
我正在尝试/除了这里:
try:
driver.find_element_by_xpath("/html/body/div[5]/div/div[1]/div[2]/div/div/article/table/tbody/tr[10]/td/a").click()
except NoSuchElementException:
driver.find_element_by_xpath("/html/body/div[5]/div/div[1]/div[2]/div/div/article/table/tbody/tr[11]/td/a").click()
我总是得到:selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:“/html/body/div[5]/div/div [1]/div[2]/div/div/article/table/tbody/tr[10]/td/span[1]"}
笔记:
- 我试过了:“尝试/除外”
- 我试过:“尝试/除了 NoSuchElementException”
- “从 selenium.common.exceptions 导入 NoSuchElementException”
- 我努力了:
try:
test = driver.find_element_by_xpath("/html/body/div[5]/div/div[1]/div[2]/div/div/article/table/tbody/tr[10]/td/a")
test_click = test.click()
except NoSuchElementException:
driver.find_element_by_xpath("/html/body/div[5]/div/div[1]/div[2]/div/div/article/table/tbody/tr[11]/td/a").click()
仍然无法使其工作。
解决方案
您可以尝试以下解决方案以避免同步错误。
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
try:
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, your element )))
except TimeoutException as ex:
print ex.message
推荐阅读
- python - 如何从 yfinance 将每个数据框重命名为股票名称
- python - 如何将 main.py 文件拆分为多个文件?
- javascript - 通过 Patch 方法更新 Yaml 文件不起作用
- javascript - VSCode 中的扩展/设置变量“#XXXXXX”的显示预览颜色
- python-3.x - String time convert to only timestamp using python
- schema - 如何在不截断输出的情况下在 Databricks 中打印模式?
- c# - FluentMigrator - 将列添加到循环中的每个表
- excel - excel合并单元格复制粘贴问题
- pytorch - 给定组=1,大小为 [32, 3, 3, 3] 的权重,预期输入 [1, 1, 32, 340] 有 3 个通道,但有 1 个通道
- python - 将 SpaCy 输出添加到现有数据框时,列不对齐