gpt4 book ai didi

python - 无论我做什么都无法使用 Selenium 选择元素

转载 作者:行者123 更新时间:2023-12-01 01:16:06 25 4
gpt4 key购买 nike

我正在尝试使用 selenium 开发一个抓取工具,但无法选择以下页面中的元素:

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

我试图获取的元素是搜索栏“Texto/Termos a serem pesquisados”

我已经尝试过

element = driver.find_element_by_id("txtTermo")

element = driver.find_element_by_name("txtTermo")

我还尝试了 xpath 和 css 选择器。

我的代码:

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")

我希望我可以获取元素,以便我可以发送 key 。

但是我收到以下错误:

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 视为单独的搜索上下文:

>>> 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")>

想象一下您可以浏览的框架层次结构,每个框架都包含自己完整的 DOM 层次结构。当您完成框架内的工作后,如果您想返回顶级文档并执行更多操作,请切换到默认内容:

driver.switch_to.default_content()

现在非常不鼓励使用框架,并且您不太可能在较新的应用程序上遇到它们,但在过去我发现(听起来很愚蠢)检查 driver.page_source 的值 可以帮助您了解当前正在处理整个文档的哪一部分。这将仅向您显示当前搜索上下文的源,即您当前切换到的框架。

关于python - 无论我做什么都无法使用 Selenium 选择元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54335017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com