MCPcopy
hub / github.com/InstaPy/InstaPy / click_element

Function click_element

instapy/util.py:1035–1119  ·  view source on GitHub ↗

There are three (maybe more) different ways to "click" an element/button. 1. element.click() 2. element.send_keys("\n") 3. browser.execute_script("document.getElementsByClassName('" + element.get_attribute("class") + "')[0].click()") I'm guessing all three have their advant

(browser, element, tryNum=0)

Source from the content-addressed store, hash-verified

1033
1034
1035def click_element(browser, element, tryNum=0):
1036 """
1037 There are three (maybe more) different ways to "click" an element/button.
1038 1. element.click()
1039 2. element.send_keys("\n")
1040 3. browser.execute_script("document.getElementsByClassName('" +
1041 element.get_attribute("class") + "')[0].click()")
1042
1043 I'm guessing all three have their advantages/disadvantages
1044 Before committing over this code, you MUST justify your change
1045 and potentially adding an 'if' statement that applies to your
1046 specific case. See the following issue for more details
1047 https://github.com/timgrossmann/InstaPy/issues/1232
1048
1049 explanation of the following recursive function:
1050 we will attempt to click the element given, if an error is thrown
1051 we know something is wrong (element not in view, element doesn't
1052 exist, ...). on each attempt try and move the screen around in
1053 various ways. if all else fails, programmically click the button
1054 using `execute_script` in the browser.
1055 """
1056
1057 try:
1058 # use Selenium's built in click function
1059 element.click()
1060
1061 # update server calls after a successful click by selenium
1062 update_activity(browser, state=None)
1063
1064 except Exception:
1065 # click attempt failed
1066 # try something funky and try again
1067
1068 if tryNum == 0:
1069 # try scrolling the element into view
1070 try:
1071 # This tends to fail because the script fails to get the element class
1072 if element.get_attribute("class") != "":
1073 browser.execute_script(
1074 "document.getElementsByClassName('"
1075 + element.get_attribute("class")
1076 + "')[0].scrollIntoView({ inline: 'center' });"
1077 )
1078 except Exception:
1079 pass
1080
1081 elif tryNum == 1:
1082 # well, that didn't work, try scrolling to the top and then
1083 # clicking again
1084 browser.execute_script("window.scrollTo(0,0);")
1085
1086 elif tryNum == 2:
1087 # that didn't work either, try scrolling to the bottom and then
1088 # clicking again
1089 browser.execute_script("window.scrollTo(0,document.body.scrollHeight);")
1090
1091 else:
1092 # try `execute_script` as a last resort

Callers 15

follow_userFunction · 0.85
get_given_user_followersFunction · 0.85
get_given_user_followingFunction · 0.85
unfollow_userFunction · 0.85
confirm_unfollowFunction · 0.85
get_follow_requestsFunction · 0.85
open_comment_sectionFunction · 0.85
dismiss_get_app_offerFunction · 0.85
dismiss_save_informationFunction · 0.85
accept_igcookie_dialogueFunction · 0.85
get_active_usersFunction · 0.85

Calls 3

update_activityFunction · 0.85
sleep_actualFunction · 0.85
execute_scriptMethod · 0.80

Tested by

no test coverage detected