Python 3.x 页面在函数的try-except语句中保持无限加载
在自动发布评论的过程中,我陷入了这样一种情况:函数有try-except语句。通过阅读下面的代码,你可能会明白我的意思 此方法写入评论并返回Python 3.x 页面在函数的try-except语句中保持无限加载,python-3.x,selenium,selenium-webdriver,automation,try-except,Python 3.x,Selenium,Selenium Webdriver,Automation,Try Except,在自动发布评论的过程中,我陷入了这样一种情况:函数有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"]')
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…')
语句后面的一行,这意味着这一行之前的单击
操作有时无法正确加载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)
继续查看发布。。。。
提交审查。。。。
提交审查。。。。
提交审查。。。。
提交审查。。。。
消息:元素不可见
(会话信息:chrome=89.0.4389.114)
(驱动程序信息:chromedriver=2.41.578700(2f1ed5f9343c13f73144538f15c00b370eda6706),平台=Linux 5.8.0-49-generic x86_64)
再次提交。。。
消息:未知错误:元素。。。在点(468,17)处不可单击。其他元素将收到单击:
(会话信息:chrome=89.0.4389.114)
(驱动程序信息:chromedriver=2.41.578700(2f1ed5f9343c13f73144538f15c00b370eda6706),平台=Linux 5.8.0-49-generic x86_64)
再次提交。。。
消息:未知错误:元素。。。在点(468,18)处不可单击。其他元素将收到单击:
(会话信息:chrome=89.0.4389.114)
(驱动程序信息:chromedriver=2.41.578700(2f1ed5f9343c13f73144538f15c00b370eda6706),平台=Linux 5.8.0-49-generic x86_64)
将打印/日志放在带有异常信息的“除外”块中,并检查异常持续上升的原因。在不记录错误消息的情况下禁用任何异常,尤其是全局异常,这真是个坏主意
例如:
try:
# Your code
except Exception as ex:
print(str(ex))
return False
将打印/日志放在带有异常信息的“除外”块中,并检查异常持续上升的原因。在不记录错误消息的情况下禁用任何异常,尤其是全局异常,这真是个坏主意 例如:
try:
# Your code
except Exception as ex:
print(str(ex))
return False
如果要防止无限循环,可以尝试以下方法
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
如果要防止无限循环,可以尝试以下方法
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
如果要单击该按钮: 进口: 添加等待:
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()
您正在使用位于的元素的存在,但它不会完全等待按钮。
接下来,移除循环并移除休眠。
用上面提到的显式等待替换所有睡眠,这将等待花费最多时间加载的元素。使用位于的元素的可见性等待元素变为可见
您使用此表单是因为某些定位器未完全加载或某些定位器不正确<代码>时间。每次输入前睡眠(1)是一个非常糟糕的解决方案。
最后,检查
a[class=“js submit”]
是否唯一。如果要单击按钮:
进口:
添加等待:
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()
您正在使用位于的元素的存在,但它不会完全等待按钮。
接下来,移除循环并移除休眠。
用上面提到的显式等待替换所有睡眠,这将等待花费最多时间加载的元素。使用位于的元素的可见性等待元素变为可见
您使用此表单是因为某些定位器未完全加载或某些定位器不正确<代码>时间。每次输入前睡眠(1)是一个非常糟糕的解决方案。
最后,检查
a[class=“js submit”]
是否唯一。那么为什么要循环它?@ArundeepChohan错误发生在print('submiting a review…')
语句之后,这意味着这一行之前的单击
操作有时无法正确加载javascript。这就是为什么我在while
loop@ArundeepChohan我刚刚添加了一些更详细的错误堆栈。为什么要在WebDriverWait之后添加sleep?为什么要在这里返回某些内容?尝试我的建议为什么要循环?@ArundeepChohan错误发生在print('Submitting a review…')
语句后的一行,这意味着单击此行之前的操作有时无法正确加载javascript。这就是为什么我在while
loop@ArundeepChohan我刚刚添加了一些更详细的错误堆栈。为什么要在WebDriverWait之后添加sleep?你为什么要在这里退货?试试我的建议谢谢你的想法。实际上,错误发生在print('Submitting a review…')
语句后面的一行,这意味着这一行之前的单击
操作有时无法正确加载javascript。这就是为什么我在之后用相同的函数调用语句再次加载该页面,而loop我还添加了错误堆栈,并进行了更多解释。谢谢您的想法。实际上,错误发生在print('Submitting a review…')
语句后面的一行,这意味着这一行之前的单击
操作有时无法正确加载javascript。这就是为什么我在之后用相同的函数调用语句再次加载该页面,而loop我还添加了错误堆栈,并进行了更多解释。