伪元素无法定位问题 NoSuchElementException: Message: no such element: Unable to locate element

Selenium+Python 定位一个伪元素的时候总是无法定位,连绝对定位都试过了,还是不行。

查了下可能是对应页面的元素还没加载完成,试着加了个显示等待时间,果然获取到了。

from selenium import webdriver
import time
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Chrome()
driver.get('https://XXXX.com/login')

driver.find_element_by_id('account').clear()
driver.find_element_by_id('account').send_keys('13120922896')
driver.find_element_by_id('password').clear()
driver.find_element_by_id('password').send_keys('KMtLcEbjfM')
driver.find_element_by_xpath('//button[@class="ant-btn src-pages-login-index-module__login-button--9qwKk ant-btn-primary"]').click()

wait = WebDriverWait(driver,5,0.5)
wait.until(lambda driver: driver.find_element_by_xpath('//div[@class="src-pages-login-index-module__shop-card--brzB-"]/button[@class="ant-btn src-pages-login-index-module__choose-btn--1x6QG ant-btn-primary"]'))
driver.find_element_by_xpath('//div[@class="src-pages-login-index-module__shop-card--brzB-"]/button[@class="ant-btn src-pages-login-index-module__choose-btn--1x6QG ant-btn-primary"]').click()

WebDriverWait(driver,timeout,poll_frequency=0.5,ignored_exceptions=None)

timeout:最长超时时间,默认以秒为单位

poll_frequency:检测的间隔时间,默认为0.5s

ignored_exceptions:超时后的异常信息,默认情况下抛出NoSuchElementException异常

until(method,message=''):调用该方法提供的驱动程序作为一个参数,直到返回值为True

原文地址:https://www.cnblogs.com/suancaipaofan/p/11799735.html