Python Selenium Webdriver点击登录按钮无响应问题求解
解决方案
触发表单校验逻辑
多数站点的登录表单会在输入框失焦时执行格式校验,校验通过后才会激活登录按钮的点击事件,你填完账号密码后需要先触发失焦:
email.send_keys("myemail@yahoo.com") password.send_keys("password") # 新增以下内容触发失焦 email.send_keys(Keys.TAB) password.send_keys(Keys.TAB) time.sleep(0.5)
更换点击触发方式
Selenium默认的DOM点击方法经常无法触发站点自定义绑定的点击事件,可换成JS点击或者动作链模拟真人点击:
next_btn = driver.find_element(By.ID, "next") # 方法一:JS强制触发点击 driver.execute_script("arguments[0].click();", next_btn) # 方法二:动作链模拟真人点击 # from selenium.webdriver.common.action_chains import ActionChains # ActionChains(driver).move_to_element(next_btn).pause(0.3).click().perform()
增加可点击状态判断
不要找到按钮就直接点击,新增显式等待确认按钮处于可交互状态:
# 点击前先等待按钮可点击 wait.until(EC.element_to_be_clickable((By.ID, "next")))
增加反爬规避配置
这类电商站点普遍有自动化工具检测机制,初始化driver时添加以下配置规避识别:
from selenium.webdriver.chrome.options import Options chrome_options = Options() # 去掉自动化标识 chrome_options.add_argument("--disable-blink-features=AutomationControlled") chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) chrome_options.add_experimental_option("useAutomationExtension", False) driver = webdriver.Remote( command_executor='http://chrome:4444/wd/hub', desired_capabilities=chrome_options.to_capabilities() ) # 覆盖navigator.webdriver属性 driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
内容的提问来源于stack exchange,提问作者user3063045
相关产品推荐
相关产品推荐

