解决Selenium的ElementClickInterceptedException报错(Upwork高级搜索)
解决Upwork高级搜索弹窗Skills Search输入框点击被拦截的问题
针对你遇到的ElementClickInterceptedException(被.up-modal-header拦截)问题,以下是几个适配新版Selenium的可行方案:
方案1:等待元素可交互后点击(优先尝试)
先确保弹窗及输入框完全加载,等待元素处于可点击状态,若直接点击失败则用JavaScript强制触发点击:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import ElementClickInterceptedException # 替换为Skills Search输入框的实际选择器(CSS/XPath均可) skills_locator = (By.CSS_SELECTOR, "input[placeholder='Add skills']") # 等待元素可点击 skills_input = WebDriverWait(driver, 10).until( EC.element_to_be_clickable(skills_locator) ) # 尝试常规点击,失败则用JS点击 try: skills_input.click() except ElementClickInterceptedException: driver.execute_script("arguments[0].click();", skills_input)
方案2:直接通过JS输入内容(跳过点击步骤)
如果你的目标是输入技能关键词,无需先点击输入框,直接用JavaScript设置输入值并触发必要事件:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By skills_locator = (By.CSS_SELECTOR, "input[placeholder='Add skills']") skills_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located(skills_locator) ) # 设置输入值 driver.execute_script("arguments[0].value = 'Python, JavaScript';", skills_input) # 触发change事件,确保Upwork识别输入内容 driver.execute_script("arguments[0].dispatchEvent(new Event('change'));", skills_input)
方案3:滚动元素避开遮挡
若.up-modal-header固定在弹窗顶部遮挡输入框,可通过滚动让输入框处于视野中心:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By skills_locator = (By.CSS_SELECTOR, "input[placeholder='Add skills']") skills_input = WebDriverWait(driver, 10).until( EC.presence_of_element_located(skills_locator) ) # 滚动到元素中心位置,避开header遮挡 driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", skills_input) # 等待元素可点击后点击 WebDriverWait(driver, 5).until( EC.element_to_be_clickable(skills_locator) ).click()
注意:请根据Upwork页面实际结构替换代码中的元素选择器,可通过浏览器开发者工具获取准确的CSS或XPath表达式。
内容的提问来源于stack exchange,提问作者Info Rewind
相关产品推荐
相关产品推荐

