【问题标题】:Can't find element with XPATH despite being found in the console search bar尽管在控制台搜索栏中找到,但无法使用 XPATH 找到元素
【发布时间】:2023-01-21 16:13:37
【问题描述】:

我正在尝试实现一个程序来填充 MS One 页面的某些部分,我一直在使用 Python 和 selenium 来完成这项任务,我可以正常访问网页,我可以在主页中发送密钥,但在客户端页面我找不到元素。

我试过了:

  • 通过.ID
  • By.XPATH(xpath 和完整 xpath)

无济于事,我为此使用的代码是:

sel = driver.find_element(By.XPATH, "//*[@id='cmtx_pasaporte_i']")
sel.send_keys("198282828")

我收到错误:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='cmtx_pasaporte_i']"}
  (Session info: chrome=109.0.5414.75

但是,当我在控制台中搜索 xpath 时,我会立即找到该元素。

我是 python、网络抓取和所有方面的新手,在此先感谢您的帮助。

console

【问题讨论】:

  • 你可以添加网址吗?另外,您确定在执行 find_element 行时该元素存在于 html 中吗?

标签: python html selenium web-scraping selenium-chromedriver


【解决方案1】:

您可以使用 Selenium 的 ExpectedConditionsWebDriverWait 类在与其交互之前显式等待元素出现。下面是一个示例,说明如何使用显式等待将文本发送到元素:

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

#Wait up to 10 seconds for the element with the ID "cmtx_pasaporte_i" to be present
wait = WebDriverWait(driver, 10)
element = wait.until(EC.presence_of_element_located((By.ID, "cmtx_pasaporte_i")))

#Once the element is present, send the text "198282828" to it
element.send_keys("198282828")

希望这能解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 2020-06-14
    • 2021-08-25
    • 2017-11-27
    • 1970-01-01
    • 2015-09-22
    相关资源
    最近更新 更多