python+selenium淘宝登录(仅供学习使用)
1.常规操作:
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome(executable_path='./chromedriver')
driver.maximize_window()
driver.get('https://login.taobao.com/')
zh = driver.find_element('id', 'fm-login-id')
pw = driver.find_element('id', 'fm-login-password')
btn = driver.find_element('xpath', '//*[@id="login-form"]/div[4]/button')
zh.send_keys('*****')
pw.send_keys('****')
btn.click()
time.sleep(0.2)
huakuai = driver.find_element('id', 'nc_1_n1z')
#动作链
action=ActionChains(driver=driver)
action.click_and_hold(huakuai)
for i in range(10):
action.move_by_offset(10,0).perform()
time.sleep(0.1)
btn.click()
2.错误:(貌似是操作太快,浏览器还未刷新)
发生异常: ElementNotInteractableException
Message: element not interactable: [object HTMLSpanElement] has no size and location (Session info: chrome=111.0.5563.65)
解决办法:
1:添加time
time.sleep(2)
问题依旧存在
2.度娘上查询了一下,说的是元素被遮挡
试了半天,发现不得行
调试发现Span宽高都为0:

摸索了一整天终于发现问题所在,
点击登录之后,需要等待一小段时间,界面出现滑块,但是,该滑块控件实际是在一个iframe中,所以就需要将driver swich一下 (问题解决)
driver.switch_to.frame('baxia-dialog-content') js = 'Object.defineProperty(navigator,"webdriver",{get:()=>false,});' huakuai = WebDriverWait(driver, 100).until( EC.presence_of_element_located((By.ID, 'nc_1_n1z'))) driver.execute_script(js) # 动作链 newaction=ActionChains(driver=driver) newaction.move_to_element(huakuai) newaction.click_and_hold(huakuai) for i in range(5): newaction.move_by_offset(65, 0).perform() time.sleep(0.1) newaction.release()
切换窗口:
mydriver.switch_to.window(mydriver.window_handles[1])
去除浏览器验证:
#去除浏览器识别 option = ChromeOptions() option.add_experimental_option('excludeSwitches', ['enable-automation']) option.add_experimental_option("detach", True) script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});' driver.execute_script(script)
浙公网安备 33010602011771号