“element is not attached to the page document“报错解决

在元素定位正确的前提下,页面报这个错说明是没有找到这个元素,加一个显示等待,直到元素可见再进行点击,这样就可以完美解决这个问题

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

loc_oper = (By.XPATH,'//span[text() = "运营管理"]/parent::div[@class = "ant-menu-submenu-title"]')
loc_op = (By.XPATH,'//span[text() = "运营管理"]/ancestor::li[@role = "menuitem"]//a[text()= "广告管理"]')

#加入等待时间

wait = WebDriverWait(driver,20)

#一直等待到元素loc_oper可见并点击
wait.until(EC.visibility_of_element_located(loc_oper))

driver.find_element(*loc_oper).click()

#一直等待到元素loc_op可见并点击
wait.until(EC.visibility_of_element_located(loc_op))

driver.find_element(*loc_op).click()

你可能感兴趣的:(Selenium,WebDriverWait,元素定位,可见性,自动化测试)