简体   繁体   English

无论我做什么都无法选择硒元素

[英]Can't select element with Selenium no matter what i do

I'm trying to develop a scraper using selenium but I can't select elements in the following page: 我正在尝试使用硒开发刮板,但无法在以下页面中选择元素:

http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp

The element i trying to fetch is the search bar "Texto/Termos a serem pesquisados" 我尝试获取的元素是搜索栏“ Texto / Termos a serem pesquisados”

I have tried 我努力了

element = driver.find_element_by_id("txtTermo")
or 要么
element = driver.find_element_by_name("txtTermo")

I also tried xpath, and css selector. 我也尝试了xpath和CSS选择器。

My Code: 我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp")

I expect that i can fetch the element so i can send keys. 我希望我可以获取元素,以便我可以发送密钥。

But i get the following error: 但我得到以下错误:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"txtTermo"} (Session info: chrome=71.0.3578.98) (Driver info: chromedriver=2.45.615279 (12b89733300bd268cff3b78fc76cb8f3a7cc44e5),platform=Linux 4.15.0-43-generic x86_64) selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“ method”:“ name”,“ selector”:“ txtTermo”}(会话信息:chrome = 71.0.3578.98)(驱动程序信息: chromedriver = 2.45.615279(12b89733300bd268cff3b78fc76cb8f3a7cc44e5),平台= Linux 4.15.0-43-通用x86_64)

The element you are trying to fetch is inside a frame. 您尝试获取的元素在框架内。 Frames are their own document and treated as a separate search context by Selenium: 框架是它们自己的文档,被Selenium视为单独的搜索上下文:

>>> from selenium import webdriver
>>> driver = webdriver.Chrome()
>>> driver.get("http://comprasnet.gov.br/acesso.asp?url=/ConsultaLicitacoes/ConsLicitacao_texto.asp")
>>> element = driver.find_element_by_id("txtTermo")
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    element = driver.find_element_by_id("txtTermo")
  File "#PATH_REDACTED#\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
    return self.find_element(by=By.ID, value=id_)
  File "#PATH_REDACTED#\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "#PATH_REDACTED#\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "#PATH_REDACTED#\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"txtTermo"}
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387),platform=Windows NT 10.0.15063 x86_64)

>>> driver.switch_to.frame('main2')
>>> element = driver.find_element_by_id("txtTermo")
>>> element
<selenium.webdriver.remote.webelement.WebElement (session="d5a30c9f45d49a23152767da0b811f58", element="0.6642244007973428-1")>

Think of a hierarchy of frames that you can navigate through, each containing their own complete DOM hierarchy. 考虑一下您可以浏览的框架层次结构,每个框架包含它们自己的完整DOM层次结构。 When you are done working inside the frame, if you want to return to the top level document and do some more work, switch to the default content: 在框架内完成工作后,如果要返回顶层文档并做更多工作,请切换到默认内容:

driver.switch_to.default_content()

Frames are highly discouraged these days and you aren't that likely to run into them on newer applications, but in the past I've found that (as silly as it sounds) examining the value of driver.page_source can help you get a handle on what part of the overall document you are currently acting on. 如今,不鼓励使用框架,而且您不太可能在较新的应用程序中遇到框架,但是在过去,我发现(听起来很愚蠢)检查driver.page_source的值可以帮助您处理问题您当前在整个文档的哪个部分上进行操作。 This will show you only the source for the current search context, ie the frame you are currently switched to. 这将仅显示当前搜索上下文的源,即当前切换到的框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM