淘宝登录需要滑块验证,但是XPATH定位元素块却报错“selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: ”,通过打印页面源码,却显示该元素块存在
报错信息:

代码如下:
from selenium import (webdriver)
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
# 打开网页
options = webdriver.ChromeOptions()
options.add_argument('disable-blink-features=AutomationControlled')
options.add_experimental_option('excludeSwitches', ['enable-automation'])
driver = webdriver.Chrome(options=options)
url = 'https://login.taobao.com/member/login.jhtml?spm=a21bo.jianhua/a.profile.1.5af92a89htDy32&f=top&redirectURL=http%3A%2F%2Fwww.taobao.com%2F'
driver.get(url)
driver.maximize_window()
# 登录
driver.implicitly_wait(5)
id = driver.find_element(By.XPATH, '//input[@id="fm-login-id"]')
password = driver.find_element(By.XPATH, '//input[@id="fm-login-password"]')
login_button = driver.find_element(By.XPATH, '//button[@class="fm-button fm-submit password-login"]')
id.send_keys('id')
password.send_keys('password')
login_button.click()
# 滑动验证
driver.implicitly_wait(5)
slider = driver.find_element(By.XPATH,
'//div[@id="nocaptcha"]/div[@id="nc_1_nocaptcha"]/div[@id="nc_1_wrapper"]/div[@id="nc_1_n1t"]/span[@id="nc_1_n1z"]')
action = ActionChains(driver)
action.click_and_hold(slider).perform()
action.move_by_offset(258, 0).perform()
action.release().perform()
# 确认保留登录信息
driver.implicitly_wait(5)
try:
driver.implicitly_wait(5)
confirm_button = driver.find_element(By.XPATH, 'button[@class="keep-login-btn keep-login-confirm-btn primary"]')
confirm_button.click()
except Exception:
pass
time.sleep(1000)
driver.quit()