Selenium报错:元素当前不可交互,马丁格尔投注脚本求助
解决Selenium中Element is not interactable错误(马丁格尔投注脚本场景)
问题描述
我正在开发一个基于马丁格尔投注策略的脚本,使用undetected_chromedriver和Selenium实现。尝试过增加等待时间、调整CSS选择器等方法,但执行bet_input.clear()时仍报错:Element is not currently interactable and may not be manipulated。
报错信息
Traceback (most recent call last):
File "C:\Users line 28, in
bet_input.clear()
Element is not currently interactable and may not be manipulated using python
相关代码
from undetected_chromedriver import Chrome, ChromeOptions from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from time import sleep # Configure undetected ChromeOptions options = ChromeOptions() options.add_argument("--disable-blink-features=AutomationControlled") options.add_argument("--disable-infobars") options.add_argument("--start-maximized") # Create a new instance of the undetected Chrome driver driver = Chrome(options=options) # Open Chrome and navigate to the website driver.get('https://rollbit.com/x-flip') # Set initial bet initial_bet = 0.50 current_bet = initial_bet sleep(5) # Main loop while True: wait = WebDriverWait(driver, 45) # Find the input field for the bet amount and enter the current bet bet_input = wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.css-p16zna input[type="text"]'))) bet_input.clear() bet_input.send_keys(str(current_bet)) # Find the button for Heads or Tails and click it bet_button = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button.css-ah57p3'))) bet_button.click() # Wait for the result sleep(10) # Or however long it typically takes for a round to complete # Check the result # If we won, reset the bet to the initial amount; if we lost, double the bet if 'You won' in result: current_bet = initial_bet else: current_bet *= 2
解决方案
替换等待条件为
element_to_be_clickable
元素可见不代表可交互,改用EC.element_to_be_clickable等待元素完全处于可操作状态:bet_input = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.css-p16zna input[type="text"]')))先点击激活输入框再执行清除
部分输入框需要先获取焦点才能操作,增加点击步骤:bet_input.click() bet_input.clear()用JavaScript强制清除内容
若常规方法无效,直接通过JS操作DOM绕过交互限制:driver.execute_script("arguments[0].value = '';", bet_input)排查页面阻塞因素
确认输入框不在iframe内(若存在需先切换iframe);同时检查是否有加载遮罩层,等待其消失:wait.until(EC.invisibility_of_element_located((By.CSS_SELECTOR, '.loading-overlay')))替换固定sleep为显式等待
去掉sleep(5)和sleep(10),改用显式等待页面核心元素加载完成:wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.css-p16zna')))
内容的提问来源于stack exchange,提问作者Jake Sowell
相关产品推荐
相关产品推荐

