【问题标题】:Page keep loading infinitly in try except statement of function页面在尝试中无限加载,除了函数语句
【发布时间】:2021-07-10 06:20:26
【问题描述】:

在自动发布评论的过程中,我遇到了一个函数有 try-except 语句的情况。阅读下面的代码,你可能会明白我的意思。

这个方法写评论并返回TrueFalse

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


【解决方案1】:

将打印/日志放在带有异常信息消息的“except”块中,并检查异常持续上升的原因。在不记录错误消息的情况下屏蔽任何异常,尤其是全局异常,这真是个坏主意。

例子:

    try:
        # Your code
    except Exception as ex:
        print(str(ex))
        return False

【讨论】:

  • 感谢您的想法。实际上,错误发生在print('Submitting a review....') 语句之后的行,这意味着此行之前的click 操作有时无法正确加载javascript。这就是为什么我在 while 循环之后使用相同的函数调用语句再次加载该页面
  • 我还添加了更多解释的错误堆栈。
【解决方案2】:

如果你想防止无限循环,你可以试试这个

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

【讨论】:

    【解决方案3】:

    如果你想点击按钮: 进口:

    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 等待元素变为可见。

    您正在添加表单,因为某些定位器未完全加载或某些定位器不正确。 time.sleep(1) 在每个输入之前是一个非常糟糕的解决方案。 最后,检查a[class="js-submit"] 是否唯一。

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-18
      • 2014-08-22
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多