如何使用Python+Selenium在Blogspot的iframe中评论?
问题:Selenium+ChromeDriver+Python 操作Blogspot评论超时
尝试在Blogspot博客发表评论时,代码在定位评论输入框环节触发TimeoutException,无法继续执行。
原代码
driver.get(url) iframe = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, 'comment-editor'))) driver.switch_to.frame(iframe) element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, 'commentBody'))) actionChains = ActionChains(driver) actionChains.move_to_element(element).click().perform() actionChains.move_to_element(element).send_keys(text).perform()
错误栈信息
Traceback (most recent call last): File "C:/Users/Hotto/PycharmProjects/blogspot/chromes.py", line 51, in <module> element=WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.NAME, 'commentBody'))) File "C:\Users\Hotto\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace: Backtrace: Ordinal0 [0x00405FD3+2187219] Ordinal0 [0x0039E6D1+1763025] Ordinal0 [0x002B3E78+802424] Ordinal0 [0x002E1C10+990224] Ordinal0 [0x002E1EAB+990891] Ordinal0 [0x0030EC92+1174674] Ordinal0 [0x002FCBD4+1100756] Ordinal0 [0x0030CFC2+1167298] Ordinal0 [0x002FC9A6+1100198] Ordinal0 [0x002D6F80+946048] Ordinal0 [0x002D7E76+949878] GetHandleVerifier [0x006A90C2+2721218] GetHandleVerifier [0x0069AAF0+2662384] GetHandleVerifier [0x0049137A+526458] GetHandleVerifier [0x00490416+522518] Ordinal0 [0x003A4EAB+1789611] Ordinal0 [0x003A97A8+1808296] Ordinal0 [0x003A9895+1808533] Ordinal0 [0x003B26C1+1844929] BaseThreadInitThunk [0x7697343D+18] RtlInitializeExceptionChain [0x77729812+99] RtlInitializeExceptionChain [0x777297E5+54]
测试URL:https://lf2011b8308.blogspot.com/2011/12/macronutrients-carbohydrates-proteins.html
解决方案
问题根源在于元素定位方式错误,且未触发评论区资源加载,修正后的代码如下:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC url = "https://lf2011b8308.blogspot.com/2011/12/macronutrients-carbohydrates-proteins.html" comment_text = "测试评论内容" driver = webdriver.Chrome() driver.get(url) # 滚动到评论区,触发编辑器iframe加载 comment_section = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "comments")) ) driver.execute_script("arguments[0].scrollIntoView();", comment_section) # 正确定位评论编辑器iframe(实际属性为id="comment-editor",而非name) editor_iframe = WebDriverWait(driver, 20).until( EC.presence_of_element_located((By.ID, "comment-editor")) ) driver.switch_to.frame(editor_iframe) # 定位评论输入框(实际元素使用class属性,而非name="commentBody") comment_box = WebDriverWait(driver, 20).until( EC.element_to_be_clickable((By.CLASS_NAME, "text_editor")) ) # 输入评论内容 comment_box.click() comment_box.send_keys(comment_text) # 如需提交评论,切换回主页面后定位提交按钮 # driver.switch_to.default_content() # submit_btn = WebDriverWait(driver, 20).until( # EC.element_to_be_clickable((By.ID, "comment-post-button")) # ) # submit_btn.click() # driver.quit()
关键修正点
- 先滚动到评论区
#comments,确保编辑器iframe被浏览器加载; - 原代码错误使用
name定位iframe,实际该iframe的唯一标识是id="comment-editor"; - 评论输入框不存在
name="commentBody"属性,改用class="text_editor"定位,且使用element_to_be_clickable确保元素可交互,比presence_of_element_located更可靠。
内容的提问来源于stack exchange,提问作者Akif
相关产品推荐
相关产品推荐

