Like the given comment
(browser, original_comment_text, logger)
| 948 | |
| 949 | |
| 950 | def like_comment(browser, original_comment_text, logger): |
| 951 | """Like the given comment""" |
| 952 | comments_block_XPath = read_xpath( |
| 953 | like_comment.__name__, "comments_block" |
| 954 | ) # quite an efficient |
| 955 | # location path |
| 956 | |
| 957 | try: |
| 958 | comments_block = browser.find_elements(By.XPATH, comments_block_XPath) |
| 959 | for comment_line in comments_block: |
| 960 | comment_elem = comment_line.find_elements(By.TAG_NAME, "span")[0] |
| 961 | comment = extract_text_from_element(comment_elem) |
| 962 | |
| 963 | if comment and (comment == original_comment_text): |
| 964 | # find "Like" span (a direct child of Like button) |
| 965 | span_like_elements = comment_line.find_elements( |
| 966 | By.XPATH, read_xpath(like_comment.__name__, "span_like_elements") |
| 967 | ) |
| 968 | if not span_like_elements: |
| 969 | # this is most likely a liked comment |
| 970 | return True, "success" |
| 971 | |
| 972 | # like the given comment |
| 973 | span_like = span_like_elements[0] |
| 974 | comment_like_button = span_like.find_element( |
| 975 | By.XPATH, read_xpath(like_comment.__name__, "comment_like_button") |
| 976 | ) |
| 977 | click_element(browser, comment_like_button) |
| 978 | |
| 979 | # verify if like succeeded by waiting until the like button |
| 980 | # element goes stale.. |
| 981 | button_change = explicit_wait( |
| 982 | browser, "SO", [comment_like_button], logger, 7, False |
| 983 | ) |
| 984 | |
| 985 | if button_change: |
| 986 | logger.info("--> Liked the comment!") |
| 987 | sleep(random.uniform(1, 2)) |
| 988 | return True, "success" |
| 989 | |
| 990 | else: |
| 991 | logger.info("--> Unfortunately, comment was not liked.") |
| 992 | sleep(random.uniform(0, 1)) |
| 993 | return False, "failure" |
| 994 | |
| 995 | except (NoSuchElementException, StaleElementReferenceException) as exc: |
| 996 | logger.error( |
| 997 | "Error occurred while liking a comment.\n\t{}".format( |
| 998 | str(exc).encode("utf-8") |
| 999 | ) |
| 1000 | ) |
| 1001 | return False, "error" |
| 1002 | |
| 1003 | return None, "unknown" |
no test coverage detected