【问题标题】:Cannot locate the textfield when python selenium script is run运行 python selenium 脚本时找不到文本字段
【发布时间】:2013-06-27 12:04:36
【问题描述】:

我在网页上有一个文本字段,我想用它来提供数据,但问题是我的 python-selenium 脚本在运行并到达该页面时无法找到它。奇怪的是,当我尝试手动定位此文本字段时,我可以毫无问题地做到这一点。我正在使用 CSSSelector 来定位该字段。

堆栈跟踪:

Traceback (most recent call last):
  File "C:/SWInstallation/TestCases/TestCases\Program.py", line 23, in test_UC_QS_FR_01_1_QuikShip_Support
bhp.enterAccDetails("0004341080", "00200")
File "C:\SWInstallation\HC-Branch\src\HomePage.py", line 24, in enterAccDetails
accInputField = self.driver.find_element_by_css_selector("#fields input[name='ACCOUNT_NUMBER']")
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 366, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 680, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 165, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 158, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: u"Unable to find element with css selector == #fields input[name='ACCOUNT_NUMBER']" 

【问题讨论】:

  • 你能分享页面的 HTML 和你用来定位元素的代码吗?通常首先要考虑的是,可能该页面尚未完全加载,因此未找到该元素。添加适当的等待可以解决问题。
  • @PocketDews:抱歉,由于我公司的限制,我无法共享 HTML 页面。但是我可以分享我用来定位元素的代码。
  • 这里是: def enterAccDetails(self, acctNumber, brNumber): accInputField = self.driver.find_element_by_css_selector("#fields input[name='ACCOUNT_NUMBER']") accInputField.send_keys(acctNumber) brInputField = self.driver.find_element_by_css_selector("#fields input[name='BRANCH']") brInputField.send_keys(brNumber) nextButton = self.driver.find_element_by_css_selector("#fields .htmlbutton") nextButton.click()
  • 有任何更新@PocketDews?
  • 您可以尝试其他定位器,例如 xpath,看看是否有帮助

标签: python selenium webdriver


【解决方案1】:

你有没有尝试过这样的事情:

e = self.driver.find_element_by_id("fields")
inputs = e.find_elements_by_css_selector("input[name='ACCOUNT_NUMBER']")

【讨论】:

  • @Praveen 你能给我一个类似于你的 HTML 代码的示例吗?我知道你无法释放关于代码的详细信息,只是我可以猜测如何访问元素的一般结构。
  • 您可以拥有任何网页,其中包含一些我很容易找到的文本字段。这个特定的文本字段有些奇怪
  • 当然;这就是我要求您提供自己的 HTML 代码的原因。无论如何,我想到了另一种方法。你试过这个吗:e = self.driver.find_element_by_id("fields"); inputs = e.find_elements_by_tag_name("input"); inputs = [element for element in inputs if element.get_attribute("name") == "ACCOUNT_NUMBER"]; desired_input = inputs[0];
猜你喜欢
  • 2011-02-09
  • 2018-03-31
  • 2018-03-01
  • 2020-11-04
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2017-02-27
相关资源
最近更新 更多