【问题标题】:Python: Unable to find element with id or class name, webdriver cannot locate id's or class name'sPython:无法找到具有 id 或类名的元素,webdriver 无法找到 id 或类名
【发布时间】:2019-01-04 00:03:58
【问题描述】:

我正在使用 Selenium 和 Python 创建一个脚本,用于在 Internet Explorer 中自动化最终用户的工作流程(必须是 IE)。使用find_element_by_idfind_element_by_class_name 时,我收到NoSuchElementException 错误。

您能帮我理解为什么我的脚本无法找到元素“servProvCode”的 ID 或名称吗?我附上了屏幕截图以提供更多详细信息。 谢谢!

第 23 行返回错误:

selenium.common.exceptions.NoSuchElementException: Message: Unable to find element with css selector == [id="submit"]

Python 脚本:

1 import time
2 from selenium import webdriver
3 from selenium.webdriver.common.by import By
4 from selenium.webdriver.common.keys import Keys
5 from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
6
7 #create capabilities
8 capabilities = DesiredCapabilities.INTERNETEXPLORER
9
10 #delete platform and version keys
11 capabilities.pop("platform", None)
12 capabilities.pop("version", None)
13
14 #start an instance of IE
15 driver = webdriver.Ie(executable_path="C:\\LocalDev\\IEDriverServer.exe", capabilities=capabilities)
16
17 #open Accela login page
18 driver.get("https://pwms-avdev.co.arapahoe.co.us/security/hostSignon.do?signOff=true")
19 time.sleep(2)
20
21 #enter Agency
22 agency = driver.find_element_by_id("servProvCode")
23 agency.send_keys('test', Keys.ENTER)
24 time.sleep(2)
25[enter image description here][1]
26 #enter Username

【问题讨论】:

  • 我没有看到附加的屏幕截图。你能再试试上传吗? HTML 直接发布到问题中会是更好的选择。
  • 文件附件过程并不简单。我以为我附上了图片,我很抱歉。这是一个指向 Dropbox 的链接,其中有 2 个屏幕截图。 dropbox.com/sh/4i3bix4aybbmc96/AACXhqGpFF7DNx5deTDl0V7ra?dl=0
  • 我在定位器中看不到任何问题。我可以闻到这可能是由于框架内的元素。在随附的屏幕截图中,无法清楚地看到元素是否在框架内。如果是,请按照这里提到的stackoverflow.com/a/40759300/2575259
  • 谢谢!,我会审查那篇文章并回复结果。
  • 感谢您的帮助 Naveen!添加“driver.switch_to.frame()”行有助于解决我遇到的问题。

标签: python selenium-webdriver internet-explorer classname


【解决方案1】:

在找到元素之前切换到框架解决了这个问题。

driver.switch_to_frame(0)
agency = driver.find_element_by_id("servProvCode")

参考资料:

in selenium web driver how to choose the correct iframe

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-28
    • 2022-01-21
    • 2018-02-21
    • 1970-01-01
    相关资源
    最近更新 更多