在评论帖子的自动化过程中,我遇到了一个问题,一个函数有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)发布于 2021-04-15 16:52:07
将打印/日志放在带有异常信息消息的'except‘块中,并检查异常持续上升的原因。在不记录错误消息的情况下静音任何异常,特别是全局异常,这真的是个糟糕的主意。
示例:
try:
# Your code
except Exception as ex:
print(str(ex))
return False发布于 2021-04-15 17:34:57
如果你想防止无限循环,你可以尝试这样做
browser.get(url)
success = write_review(browser,row)
if success==False:
trytimes = 0
maxtrytimes = 10
while success!=True:
browser.get(url)
print('Again submitting...')
success = write_review(browser,row)
trytimes+=1
if trytimes>maxtrytimes:
print("write_review failed too many times")
break发布于 2021-04-15 19:25:12
如果要单击按钮:导入:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By添加等待:
wait = WebDriverWait(driver, timeout=30)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".a[class="js-submit"]")))
list_view = driver.find_element_by_css_selector('a[class="js-submit"]')
list_view.click()您正在使用presence_of_element_located,但它不会完全等待按钮。接下来,删除循环并删除睡眠。将所有睡眠替换为如上所述的显式等待,这将等待加载时间最长的元素。使用EC.visibility_of_element_located等待元素变为可见。
您正在对表单执行ddosing操作,因为某些定位器未完全加载或某些定位器不正确。在每次输入之前使用time.sleep(1)是一个非常糟糕的解决方案。最后,检查a[class="js-submit"]是否是唯一的。
https://stackoverflow.com/questions/67104631
复制相似问题