【问题标题】:Can't find a button 'ctl00' with selenium找不到带有硒的按钮“ctl00”
【发布时间】:2016-06-29 23:19:29
【问题描述】:

从此页面:

https://permits.losgatosca.gov/CitizenAccess/default.aspx

我正在尝试按下建筑许可证下的“搜索许可证”按钮。

这是它的 xpath:

//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]/span

这是我正在使用的代码:

url = "https://permits.losgatosca.gov/CitizenAccess/default.aspx"


driver_1 = webdriver.Firefox()
driver_1.get(url)

NEXT_BUTTON_XPATH = '//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]/span'
# "//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]/span"
button = driver_1.find_element_by_xpath(NEXT_BUTTON_XPATH)
button.click()

但我收到了这条消息:

selenium.common.exceptions.NoSuchElementException:消息:无法 定位元素: {"方法":"xpath","选择器":"//*[@id=\"ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl\"]/span"} 堆栈跟踪:

【问题讨论】:

  • “这是它的 xpath”。你怎么知道那是正确的 xpath?
  • id="span_link_0"#span_link_0
  • 这不是 xpath。再次检查页面来源
  • @RemcoW 我正在使用 chrome 获取 Xpath。可能是错的
  • 它在frame.. 你需要先切换那个框架而不是去寻找像driver_1.switch_to_frame("ACAFrame")这样的元素

标签: python selenium


【解决方案1】:

有一个id为ACAFrame的框架,你需要先切换那个框架,如下:-

driver_1.switch_to_frame("ACAFrame")
NEXT_BUTTON_XPATH = '//*[@id="ctl00_PlaceHolderMain_TabDataList_TabsDataList_ctl01_LinksDataList_ctl00_LinkItemUrl"]/span'
button = driver_1.find_element_by_xpath(NEXT_BUTTON_XPATH)

已编辑..

点击Search Permits对打开的表单执行操作后,您应该尝试如下:-

driver_1.switch_to.default_content()
driver_1.switch_to_frame("ACAFrame")
# now find your desire element 

希望它会起作用...:)

【讨论】:

  • 我想我在按下按钮后加载的窗口中遇到了同样的错误。我正在尝试在“街道名称:”文本框中输入内容。得到这个:消息:无法定位元素:{"method":"id","selector":"//*[@id=\"ctl00_PlaceHolderMain_generalSearchForm_txtGSStreetName\"]"}
  • 我应该在一个新问题中问这个问题吗?
  • 需要再次切换该帧默认driver_1.switch_to.default_content()而不是driver_1.switch_to_frame("ACAFrame")
猜你喜欢
  • 2023-02-24
  • 2021-10-03
  • 2021-09-06
  • 2016-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 2017-12-17
相关资源
最近更新 更多