selenium定位li标签无法 定位

问题遇到的现象和发生背景

在尝试利用selenium做自动化时,利用find_element(By.XPATH,)代码定位li标签时却返回无法找到属性值的错误。click()没有属性去点击。Xpath是html网页复制来的

问题相关代码,请勿粘贴截图
btn2 = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div[1]/div/ul[1]/li[1]')
print(btn2)
sleep(2)
btn2.click()

运行结果及报错内容
Traceback (most recent call last):
  File "C:\Users\Susu\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
    return self.execute(Command.FIND_ELEMENT, {
  File "C:\Users\Susu\AppData\Roaming\Python\Python310\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Susu\AppData\Roaming\Python\Python310\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":"xpath","selector":"/html/body/div[1]/div[3]/div[1]/div/ul[1]/li[1]"}
  (Session info: MicrosoftEdge=104.0.1293.70)

我的解答思路和尝试过的方法

翻找过网页源代码不存在iframe 和tbody标签。

img

发现第二个li标签的class值会随着我点击“章节”而加上第一个 li标签的“curNav”的值

我想要达到的结果

可以通过By.xpath 解析到第二个li标签

可以给出具体的url地址吗?
根据你的问题既然定位到了xpath显示没有找到目标
原因可能是你没有切换句柄

你这么提问是没用的,至少给出要访问的url地址

http://i.chaoxing.com/ 这是代码的url 是进入里面课程的页面代码

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
# 实现规避检测
from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge

# 实现规避检测
option = EdgeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])

# 如何实现让selenium规避被检测到的风险
driver = webdriver.Edge(executable_path='./MicrosoftWebDriver.exe')

driver.get("http://i.chaoxing.com/base?t=1661779724086")

search_input = driver.find_element(By.CLASS_NAME,'ipt-tel')
search_input.send_keys('13')
sleep(1)
search_input = driver.find_element(By.ID,'pwd')
search_input.send_keys('sd.')
sleep(1)
btn = driver.find_element(By.ID,'loginBtn')
btn.click()
sleep(2)
driver.switch_to.frame('frame_content')
btn1 = driver.find_element(By.CLASS_NAME,'course-name')
btn1.click()
sleep(5)
btn2 = driver.find_element(By.XPATH,'/html/body/div[1]/div[3]/div[1]/div/ul[1]/li[1]')
print(btn2)
sleep(2)
btn2.click()

完整代码 其中问题代码是后4行