【发布时间】:2021-07-10 06:20:26
【问题描述】:
在自动发布评论的过程中,我遇到了一个函数有 try-except 语句的情况。阅读下面的代码,你可能会明白我的意思。
这个方法写评论并返回True或False
def write_review(browser,row):
try:
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'label[for="rating-5"]'))).click()
sleep(1)
browser.find_element_by_id("review.userNickname").clear()
browser.find_element_by_id("review.userNickname").send_keys(row['nickname'].replace(' ',''))
sleep(1)
browser.find_element_by_id("review.title").clear()
browser.find_element_by_id("review.title").send_keys(row['headline'])
sleep(1)
browser.find_element_by_id("review.reviewText").clear()
browser.find_element_by_id("review.reviewText").send_keys(row['review'])
sleep(1)
browser.find_element_by_css_selector('label[for="review.netPromoterScore.10"]').click()
sleep(1)
browser.find_element_by_css_selector('button[class*="js-preview"]').click()
sleep(3)
print('Submitting a review....')
WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'a[class*="js-submit"]'))).click()
sleep(5)
return True
except Exception as ex::
print(str(ex))
return False
如果任何评论没有成功发布,我已经实现了这样的函数调用
browser.get(url)
success = write_review(browser,row)
if success==False:
while success!=True:
browser.get(url)
print('Again submitting...')
success = write_review(browser,row)
但是上面代码的问题是,如果评论发布不成功,页面会不断加载,而没有在评论正文中输入任何数据。实际上,错误发生在print('Submitting a review....') 语句之后的行,这意味着此行之前的click 操作有时无法正确加载javascript。这就是为什么我在while 循环之后使用相同的函数调用语句再次加载该页面。以下是一些错误信息
Continue to review posting....
Submitting a review....
Submitting a review....
Submitting a review....
Submitting a review....
Message: element not visible
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
Again submitting...
Message: unknown error: Element <label for="rating-5" class="">...</label> is not clickable at point (468, 17). Other element would receive the click: <input id="search-autocomplete" type="text" name="query" autocomplete="off" autocorrect="off" placeholder="Search food, toys, prescriptions and more" role="combobox" aria-controls="search__suggestions" aria-haspopup="listbox" aria-label="Search food, toys, prescriptions and more" class="sfw-search__input">
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
Again submitting...
Message: unknown error: Element <label for="rating-5" class="">...</label> is not clickable at point (468, 18). Other element would receive the click: <input id="search-autocomplete" type="text" name="query" autocomplete="off" autocorrect="off" placeholder="Search food, toys, prescriptions and more" role="combobox" aria-controls="search__suggestions" aria-haspopup="listbox" aria-label="Search food, toys, prescriptions and more" class="sfw-search__input">
(Session info: chrome=89.0.4389.114)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 5.8.0-49-generic x86_64)
【问题讨论】:
-
你为什么要循环播放它?
-
@ArundeepChohan 错误发生在
print('Submitting a review....')语句之后的行,这意味着此行之前的click操作有时无法正确加载javascript。这就是为什么我在while循环之后使用相同的函数调用语句再次加载该页面 -
@ArundeepChohan 我刚刚添加了更多详细信息的错误堆栈。
-
你为什么在 WebDriverWait 之后设置睡眠?为什么要在这里退货?
-
试试我的建议
标签: python-3.x selenium selenium-webdriver automation try-except