Python selenium错误“没有这样的元素:找不到元素””

iqxoj9l9  于 2023-01-26  发布在  Python
关注(0)|答案(1)|浏览(148)

我已经尝试了几个小时,点击网页上的一个按钮,下面的代码,但无法找到解决方案。
我试着用Xpath,full-Xpath,class来点击它,但是不起作用。我听说“iframe”可能会出错,但是我没有看到任何名为“frame”的标记(或者iframe标记可能在文档中使用不同的名称??)
供您参考,当我按下按钮时,它并不指向新页面,而是出现一个弹出窗口来填写内容。

错误消息

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"btnReleaseVendorInfoModal"}
  (Session info: chrome=98.0.4758.102)

按钮

<button type="button" class="btn btn-warning btn-sm" id="btnReleaseVendorInfoModal"> 반출기본정보 수정</button>

Python代码

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
from selenium.common.exceptions import StaleElementReferenceException

# Login

# put id
driver = webdriver.Chrome()
driver.get("https://po-management.net/release/list")
time.sleep(1)
elem = driver.find_element_by_name("username")
elem.send_keys(usernameStr)
elem.send_keys(Keys.RETURN)

time.sleep(2)

# put password
password = driver.find_element_by_xpath('//*[@id="input73"]')
try:
    ActionChains(driver).send_keys(Keys.TAB).send_keys(passwordStr).perform()
    password.send_keys(passwordStr)
except StaleElementReferenceException:
    pass
password.send_keys(Keys.RETURN)

# redirect
time.sleep(3)
url = "https://po-management.net/release/list"
driver.get(url)

# search
time.sleep(1)
search = driver.find_element_by_id("releaseSeqArray")
search.send_keys(vrorder)
search.send_keys(Keys.RETURN)

# click1
time.sleep(1)
driver.find_element_by_link_text(vrorder).click()

# click2
time.sleep(3)
driver.find_element_by_link_text("btnReleaseVendorInfoModal").click()
iq3niunx

iq3niunx1#

要在***clickable***元素上执行click(),需要为element_to_be_clickable()引入WebDriverWait,可以使用以下locator strategies之一:

  • 使用 * CSS选择器 *:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-warning.btn-sm#btnReleaseVendorInfoModal"))).click()
  • 使用 * XPATH *:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-warning btn-sm' and @id='btnReleaseVendorInfoModal']"))).click()
  • 使用 * XPATH * 和 * 内部文本 *:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[normalize-space(text())='반출기본정보 수정']"))).click()
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

参考文献

您可以在以下位置找到一些关于NoSuchElementException的相关讨论:

相关问题