Created
March 27, 2018 11:27
-
-
Save uluQulu/6ca40814369846b7ba1ea3fdd1b2a5b8 to your computer and use it in GitHub Desktop.
Comment problem with updating reactJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import choice | |
from .time_util import sleep | |
from .util import update_activity | |
from .util import add_user_to_blacklist | |
from selenium.common.exceptions import WebDriverException | |
import emoji | |
from selenium.webdriver.common.keys import Keys | |
def get_comment_input(browser): | |
comment_input = browser.find_elements_by_xpath( | |
'//textarea[@placeholder = "Add a comment…"]') | |
if len(comment_input) <= 0: | |
comment_input = browser.find_elements_by_xpath( | |
'//input[@placeholder = "Add a comment…"]') | |
return comment_input | |
def open_comment_section(browser): | |
missing_comment_elem_warning = ( | |
'--> Warning: Comment Button Not Found:' | |
' May cause issues with browser windows of smaller widths') | |
comment_elem = browser.find_elements_by_xpath( | |
"//a[@role='button']/span[text()='Comment']/..") | |
if len(comment_elem) > 0: | |
try: | |
browser.execute_script( | |
"arguments[0].click();", comment_elem[0]) | |
except WebDriverException: | |
print(missing_comment_elem_warning) | |
else: | |
print(missing_comment_elem_warning) | |
def comment_image(browser, username, comments, blacklist, logger, logfolder): | |
"""Checks if it should comment on the image""" | |
rand_comment = (choice(comments).format(username)) | |
rand_comment = emoji.demojize(rand_comment) | |
rand_comment = emoji.emojize(rand_comment, use_aliases=True) | |
open_comment_section(browser) | |
comment_input = get_comment_input(browser) | |
if len(comment_input) > 0: | |
comment_input[0].clear() | |
comment_input = get_comment_input(browser) | |
browser.execute_script( | |
"arguments[0].value = '" + rand_comment + " ';", comment_input[0]) | |
# An extra space is added here and then deleted. | |
# This forces the input box to update the reactJS core | |
comment_input[0].send_keys("\b") | |
comment_input = get_comment_input(browser) | |
comment_input[0].submit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment