You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium驱动无法定位并操作QBCC网站输入框问题求助

QBCC网站爬虫输入框定位失败问题解决方案

问题背景

正在编写爬虫抓取QBCC网站(https://my.qbcc.qld.gov.au/myQBCC/s/findlocalcontractor),已成功处理页面两个下拉菜单,但无法定位并操作目标输入框。该输入框HTML标签如下:

<input lwc-enmikoh2qu="" class="slds-input" aria-errormessage="help-message-54" id="input-54" name="find" part="input" placeholder="Type suburb, post code here..." type="text">

已尝试的失败方法

  • 通过ID定位,但ID会频繁变化,提示元素未找到
  • 通过类名、name属性、CSS选择器、XPATH、标签名定位,均提示元素未找到
  • 使用JavaScript获取元素,返回None,无法点击或输入内容
  • 使用隐式等待和显式等待等待元素加载,仍提示元素未找到

最后尝试的代码片段

location = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[class*="slds-input"]')))
#location = driver.execute_script('return document.querySelector(\'input[placeholder="Type suburb, post code here..."]\');')
location.click()
location.send_keys('New Auckland')
location.send_keys(Keys.ENTER)

完整参考代码

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

URL = "https://my.qbcc.qld.gov.au/myQBCC/s/findlocalcontractor"

driver = webdriver.Chrome()
driver.get(URL)

time.sleep(8)

industry = Select(driver.find_element(By.ID, '28:2;a'))
industry.select_by_index(4)

range = Select(driver.find_element(By.ID, '46:2;a'))
range.select_by_index(3)

time.sleep(1)

location = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[class*="slds-input"]')))
#location = driver.execute_script('return document.querySelector(\'input[placeholder="Type suburb, post code here..."]\');')
location.click()
location.send_keys('New Auckland')
location.send_keys(Keys.ENTER)

time.sleep(3)

page_source = driver.page_source

driver.quit()

soup = BeautifulSoup(page_source, "html.parser")

解决方案

1. 检查并切换iframe

该网站基于Salesforce LWC构建,大概率存在iframe嵌套。输入框可能在iframe内,需先切换到对应iframe再定位:

# 等待iframe加载并切换
iframe = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to.frame(iframe)

# 之后再定位输入框
location = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type suburb, post code here..."]')))

2. 使用多属性组合定位

结合多个稳定属性编写定位器,提升准确性:

  • CSS选择器:input.slds-input[name="find"][placeholder="Type suburb, post code here..."]
  • XPATH://input[@class="slds-input" and @name="find" and @placeholder="Type suburb, post code here..."]

代码示例:

location = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input.slds-input[name="find"][placeholder="Type suburb, post code here..."]')))
location.click()
location.send_keys('New Auckland')
location.send_keys(Keys.ENTER)

3. 用JavaScript直接操作元素

如果元素存在但无法点击,直接通过JS设置值并触发事件:

location = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'input.slds-input[name="find"]')))
# 设置输入值
driver.execute_script("arguments[0].value = 'New Auckland';", location)
# 触发回车提交
driver.execute_script("arguments[0].dispatchEvent(new KeyboardEvent('keypress', {key: 'Enter'}));", location)

4. 替换固定sleep为显式等待

避免使用time.sleep(),改用显式等待确保下拉菜单操作完成后再处理输入框:

# 等待第一个下拉菜单加载并选择
industry_select = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, '28:2;a')))
industry = Select(industry_select)
industry.select_by_index(4)

# 等待第二个下拉菜单加载并选择
range_select = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, '46:2;a')))
range = Select(range_select)
range.select_by_index(3)

# 等待输入框可操作
location = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input.slds-input[name="find"]')))

内容的提问来源于stack exchange,提问作者SolidOpt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 07:05:55