无法通过任何属性找到元素

问题描述 投票:0回答:1

解释

我有一个想要单击的链接,但该链接隐藏在隐藏的汉堡菜单下。我尝试单独单击该链接,但没有成功。所以我尝试先打开汉堡菜单,然后搜索该元素,但它似乎连汉堡菜单按钮都找不到。

HTML

<nav id="ember4" class="left-menu set-aria-hidden ember-view"><div class="FlexWrapper">
<div>
  <button aria-label="main navigation menu" class="menu-icon " type="button" data-ember-action="" data-ember-action-5="5">
    <span class="icon-menu_hamburger"></span>
    <span class="menu-text ">MENU</span>
  </button>
</div>
...
</nav>

代码

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException


options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)

driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))
driver.get("https://connect.mheducation.com/connect/hmStudentCourseList.do?showSniffer=true&fromPage=login")

try:
    WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, 'login-email')))
except TimeoutException as ex:
    print(ex)

driver.find_element(By.ID, "login-email").send_keys("CREDENTIALS")
driver.find_element(By.ID, "login-password").send_keys("CREDENTIALS")
driver.find_element(By.ID, "login-submit-btn").click()

try:
    WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="list_2747337_item_15168696_text"]/div[3]/div/div[2]/a')))
except TimeoutException as ex:
    print(ex)

driver.find_element(By.XPATH, '//*[@id="list_2747337_item_15168696_text"]/div[3]/div/div[2]/a').click()

try:
    WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.XPATH, '//*[@id="ember4"]/div/div[1]/button')))
except TimeoutException as ex:
    print(ex)

driver.find_element(By.XPATH, '//*[@id="ember4"]/div/div[1]/button').click()
driver.find_element(By.XPATH, '//*[@id="ToDoMenu"]/a').click()

错误

Traceback (most recent call last):
  File "C:\Users\Onur\Documents\HomeworkSolver\homeworkSolver.py", line 36, in <module>
    driver.find_element(By.XPATH, '//*[@id="list_2747337_item_15168696_text"]/div[3]/div/div[2]/a').click()
  File "C:\Users\Onur\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 94, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\Onur\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webelement.py", line 395, in _execute
    return self._parent.execute(command, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Onur\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 347, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Onur\AppData\Local\Programs\Python\Python312\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 229, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: no such execution context
  (Session info: chrome=123.0.6312.106)

从错误代码来看,它似乎在查找我一直尝试单击的实际元素之前的元素时遇到问题,但它仍然单击具有 XPATH 的元素

'//*[@id="list_2747337_item_15168696_text"]/div[3]/div/div[2]/a'

然后移动到图片显示的最后一页。

这里有什么问题吗?如果找不到该元素如何单击它?这是我找不到我要单击的导航栏按钮的实际原因吗?

注释

Python 3.12.2

硒4.19.0

python-3.x selenium-webdriver
1个回答
0
投票

在让元素可见并在 Chrome 中检查它之后,您是否厌倦了使用元素的完整 xpath?这对我来说很有效。让我知道这是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.