selenium.common.exceptions.NoSuchElementException:消息:无法定位元素 [id=""]

neone315

我正在尝试使用 selenium 获取输入标签并使用 click() 。

这是我的代码:

#Try many ways to fix the error
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import selenium.webdriver.support.ui as ui
import time

driver = webdriver.Firefox()
wait = ui.WebDriverWait(driver, 10)
driver.get("""https://www.gso.gov.vn/px-web-2/?pxid=V0641&theme=N%C3%B4ng%2C%20l%C3%A2m%20nghi%E1%BB%87p%20v%C3%A0%20th%E1%BB%A7y%20s%E1%BA%A3n""")
driver.maximize_window() # For maximizing window

time.sleep(10)
selectButton= driver.find_element(By.ID,'footer')
print("select button : ", selectButton)
print("select button : ", selectButton.text)


time.sleep(3)
driver.close()

它工作正常,这里的输出:

select button :  <selenium.webdriver.firefox.webelement.FirefoxWebElement (session="e94aba42-01a7-467b-81b5-16ce0b95dad8", element="32699e02-2621-4090-be18-2dfeac33b1c2")>
select button :  TRANG THÔNG TIN ĐIỆN TỬ TỔNG CỤC THỐNG KÊ
Bản quyền thuộc Tổng cục Thống kê
Địa chỉ: 54 Nguyễn Chí Thanh, Đống Đa, Hà Nội
Điện thoại: 024 73046666, máy lẻ 8668
Email: [email protected]
Ghi rõ nguồn trang Thông tin điện tử Tổng cục Thống kê (www.gso.gov.vn) khi trích lại thông tin từ địa chỉ này

selectButton通过将 id 字符串替换为:

selectButton= driver.find_element(By.ID,'ctl00_ContentPlaceHolderMain_VariableSelector1_VariableSelector1_VariableSelectorValueSelectRepeater_ctl01_VariableValueSelect_VariableValueSelect_SelectAllButton')

我得到了错误:

Traceback (most recent call last):
  File "crawl.py", line 17, in <module>
    selectButton= driver.find_element(By.ID,'ctl00_ContentPlaceHolderMain_VariableSelector1_VariableSelector1_VariableSelectorValueSelectRepeater_ctl01_VariableValueSelect_VariableValueSelect_SelectAllButton')
  File "/home/dongky/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "/home/dongky/.local/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/home/dongky/.local/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="ctl00_ContentPlaceHolderMain_VariableSelector1_VariableSelector1_VariableSelectorValueSelectRepeater_ctl01_VariableValueSelect_VariableValueSelect_SelectAllButton"]

不知道为什么,网站结构有问题,或者id名称太长?谢谢阅读!

游轮潘迪

您正在寻找的元素在iframe 中因此,我们必须更改驱动程序焦点才能与期望元素或多个元素进行交互:

iframe xpath :

//iframe[contains(@src, 'https://pxweb.gso.gov.vn/pxweb/vi')]

并像这样切换:

WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[contains(@src, 'https://pxweb.gso.gov.vn/pxweb/vi')]")))

在此之后,您可以继续执行此操作:

selectButton= driver.find_element(By.ID,'ctl00_ContentPlaceHolderMain_VariableSelector1_VariableSelector1_VariableSelectorValueSelectRepeater_ctl01_VariableValueSelect_VariableValueSelect_SelectAllButton')

但是我建议您使用更可靠的定位器而不是上述 ID。

进口:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 selenium 在文本字段中设置值。selenium.common.exceptions.NoSuchElementException:消息:无法定位元素:

chromedriver selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素(XPath)

selenium.common.exceptions.NoSuchElementException:消息:无法定位元素错误,使用 Selenium Python 将文本发送到 Twitter 中的电子邮件字段

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法使用 Selenium 和 Python 定位元素

selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:

NoSuchElementException:消息:尝试使用Selenium和Python定位元素时无法定位元素

NoSuchElementException:消息:尝试通过 Selenium 和 Python 单击 VISA 按钮时无法定位元素

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:

selenium.common.exceptions.NoSuchElementException:消息:无法找到元素:// pre / * [not(self :: b)]

selenium.common.exceptions.NoSuchElementException:消息:即使 HTML 中存在 ID 和名称,Python selenium 中也没有此类元素错误

如何修复selenium.common.exceptions.NoSuchElementException?

selenium.common.exceptions.TimeoutException:消息:

Selenium无法定位元素(Python)

Selenium 无法定位元素

无法定位元素 _ Python Selenium

Python Selenium 无法定位元素

无法找到按钮,selenium.common.exceptions.NoSuchElementException

selenium.common.exceptions.ElementClickInterceptedException:消息:拦截了元素单击:Selenium和Python无法单击元素

selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:尝试用selenium单击“下一步”按钮时无法找到元素

selenium.common.exceptions.ElementClickInterceptedException 问题:消息:元素点击被拦截:

selenium.common.exceptions.ElementNotInteractableException:消息:元素未显示

selenium.common.exceptions.ElementNotVisibleException:消息:元素无法使用Selenium进行交互

Selenium webdriver 消息:没有这样的元素:无法定位元素 - iFrame

Selenium 无法定位字段集中的定位元素

Selenium 无法通过 id/name 定位元素

动态内容中的selenium.common.exceptions.NoSuchElementException

使用Chrome时出现硒“ selenium.common.exceptions.NoSuchElementException”

Selenium.common.exceptions.NoSuchElementException错误,即使显式等待