Basically i am creating a bot for a learning purpose, what i am trying to do is user add commas with comment to spin the comments, but it's not working if user add in comment box like hey, "amazing" "cool", then the bot comments in random words like "h","y","I, basically it's randomizing the first character,
Here are the codes
def comment(driver, comment_custom,hashtags,count):
url = "https://www.instagram.com/explore/tags/" hashtags
driver.get(url)
wait = WebDriverWait(driver,10)
path = "/html/body/div[1]/section/main/article/div[1]/div/div/div[1]/div[1]/a/div/div[2]"
first_photo = wait.until(EC.presence_of_element_located((By.XPATH,path)))
first_photo.click()
time.sleep(1)
next_button1st = driver.find_element_by_xpath("/html/body/div[6]/div[1]/div/div/div/button")
next_button1st.click()
time.sleep(1)
for i in range (int(count)):
#comments on photo
path = "/html/body/div[6]/div[2]/div/article/div/div[2]/div/div/div[2]/section[3]/div/form/textarea"
comment = wait.until(EC.presence_of_element_located((By.CLASS_NAME,"Ypffh")))
comment.click()
time.sleep(1)
commet_text = wait.until(EC.presence_of_element_located((By.CLASS_NAME, "Ypffh")))
comment = comment_custom[randint(0, len(comment_custom)-1)]
commet_text.send_keys(comment)
#post comment button
post_button = wait.until(
EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Post')]")))
post_button.click()
time.sleep(1)
next_button2st = wait.until(EC.presence_of_element_located((By.XPATH,"/html/body/div[6]/div[1]/div/div/div[2]/button")))
next_button2st.click()
and this is the code of tkinter
Comments_with_hash = Button(root, text="Comments_with_hashtags", height=1, width = 30,
command=lambda:comment(driver,comments.get(),hashtag.get(),likecount.get()))
Comments_with_hash.grid(padx=5,pady=5)
And here is the photo of bot https://prnt.sc/21k1nsr.png
Expected comments i want
Nice and then Amazing and then amazing and nice basically in random
EDITED Not able to randomize the sleep time basically what i want to do it user type 2 numbers like 1,5 to randomize the time between each operation. Bot Image :https://prnt.sc/21nj6xm codes i am using
time.sleep(randint(int(delay)))
Error
time.sleep(randint(int((delay))))
ValueError: invalid literal for int() with base 10: '1,5'
CodePudding user response:
I can't run but I guess:
comment_custom
is single string so using index comment_custom[index]
you get single char from string.
comment_custom = "hey,amazing,cool"
print( comment_custom[0] ) # char `h`
You have to convert it to list of strings/words and then select word from list
comment_custom = "hey,amazing,cool"
words = comment_custom.split(',')
print( words[0] ) # string `hey`
You could use random.choice(words)
instead of words[random.randint(0, len(words)-1)]
So you should do
words = comment_custom.split(',')
comment = random.choice(words)
commet_text.send_keys(comment)