小菜鸡在尝试扒拉12306的余票信息,卡在了模拟点击查看票价的地方
代码:
myshowprice=mybrowser.find_element(By.ID,"queryLeftTable")
myprices=myshowprice.find_elements(By.CLASS_NAME,"lookup")
for myprice in myprices:
myprice.click()
元素定位到:
<span id="5l00000G2410_01_05_uAWTjGjz5yxiej7XrXHmUy5RLe0k4C9xhU0hsoiPj6SUVNIK_9MO" class="lookup" onclick="showTicketPrice(this)"><span style="display:none;">查看票价</span><b title="查看票价" tabindex="0" aria-label="查看票价,按回车键操作"></b></span>
<span id="5l00000G2810_01_03_bJ%2BcRc7BSFvgLZnMNeUu1RI9Jm0CvMbjnvr6Zo9myHV%2BPhxG_9MO" class="lookup" onclick="showTicketPrice(this)"><span style="display:none;">查看票价</span><b title="查看票价" tabindex="0" aria-label="查看票价,按回车键操作"></b></span>
myprice.click() 报错:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable: element has zero size
因为你没定位到,所以返回的是个空列表
【以下回答由 GPT 生成】
这个问题可能是由于元素没有正确加载或不可见引起的。以下是一些可能的解决方案:
WebDriverWait
类来实现这一点。以下是一个示例代码:from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 等待元素可见
wait = WebDriverWait(mybrowser, 10)
myshowprice = wait.until(EC.visibility_of_element_located((By.ID, "queryLeftTable")))
myprices = myshowprice.find_elements(By.CLASS_NAME, "lookup")
for myprice in myprices:
myprice.click()
execute_script
方法将元素滚动到可视区域内。以下是一个示例代码:from selenium.webdriver.common.action_chains import ActionChains
# 滚动元素到可视区域内
actions = ActionChains(mybrowser)
actions.move_to_element(myshowprice).perform()
myprices = myshowprice.find_elements(By.CLASS_NAME, "lookup")
for myprice in myprices:
myprice.click()
execute_script
方法模拟点击事件。以下是一个示例代码:# 模拟点击事件
myprices = myshowprice.find_elements(By.CLASS_NAME, "lookup")
for myprice in myprices:
mybrowser.execute_script("arguments[0].click();", myprice)
如果以上解决方案都不能解决问题,可能是由于页面上的其他因素导致无法与元素进行交互。在这种情况下,你可以尝试使用其他方法定位并点击元素,或者在问题描述中提供更多的代码和页面结构信息以供进一步分析。
【相关推荐】