I am using python3, and want to send messages in Instagram using python, package selenium.
the code works great before. Today most of the time the send_keys() does not work.
I increase the sleep() time to be sure that it is not the internet speed problem. still, it does not work. Any idea? or any alternative ways to send text in a box (I am using python)?
this is the code:
webdriver.get('https://www.instagram.com/explore/tags/'+ hashtag_list[tag] + '/')
sleep(5)
check='//*[#id="react-root"]/section/main/article/div[2]/div/div[%d]/div[%d]/a/div' %(i,x)
sleep(10)
#'//*[#id=\"react-root\"]/section/main/article/div[1]/div/div/div[1]/div[%s]/a/div' % (x)
first_thumbnail = webdriver.find_element_by_xpath(check)
first_thumbnail.click()
sleep(10)
webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[1]/span[2]/button/span').click()
sleep(10)
comment_box = webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[3]/div/form/textarea')
sleep(20)
comment_box.send_keys("Hi") # this line does not work
sleep(10)
webdriver.find_element_by_xpath('/html/body/div[3]/div[2]/div/article/div[2]/section[3]/div/form/button').click()
sleep(10)
except:
print("Oops!")
sleep(10)
continue
Have you checked to make sure comment_box is actually a valid webelement by printing it? Just to make sure the selector is correct?
Because it should normally work, but perhaps Instagram is somehow doing something to block it.
Related
i trying to create a small program usign python and i need some help about python loops.
this small program will automates a fairly boring repetitive task.
I use module : selenium, time and pyautogui
here is the piece of code that i want to repeat until it no longer finds a certain element on the web page :
btnOptions = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div[2]/div/div[2]/div[3]/div[1]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div[2]/div/div[3]/div/div")
btnOptions.click()
time.sleep(1)
pyautogui.press("down", presses=10)
pyautogui.press("enter")
time.sleep(1)
btn_move = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[6]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div[3]/div/div/div/div[1]")
btn_move.click()
as long as the btn_option is found, the program must continue otherwise it must stop.
I can't find a solution, if anyone can help me it would be very appreciated.
thanks a lot
i tried several loops but every time i get an error of course, I'm just new to coding in python :/
I've done a similar task. You may try this:
while True:
try:
btnOptions = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[5]/div/div/div[3]/div/div/div[1]/div[1]/div/div/div[4]/div[2]/div/div[2]/div[3]/div[1]/div/div/div/div/div/div/div/div/div/div/div[8]/div/div[2]/div/div[3]/div/div")
btnOptions.click()
time.sleep(1)
pyautogui.press("down", presses=10)
pyautogui.press("enter")
time.sleep(1)
btn_move = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[1]/div/div[6]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div[3]/div/div/div/div[1]")
btn_move.click()
except NoSuchElementException:
break
I would like to create a bot which will retweet all tweet load on an account.
First I try to use this method:
rt = self.driver.find_elements_by_xpath("//div[#data-testid='retweet']")
for i in rt:
time.sleep(1)
i.click()
"""Confirm the retweet"""
time.sleep(1)
self.driver.find_element_by_xpath("//div[#data-testid='retweetConfirm']").click()
But it didn't work, I wasn't able to wait enought before clicking, even with a high time.sleep().
So instead, I try this:
for i in rt:
z=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#data-testid='retweet']")))
z.click()
#Confirm the retweet
b=WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//div[#data-testid='retweetConfirm']")))
b.click()
Unfortunately this code will only retweet the first tweet. I should modify something in EC.element_to_be_clickable((By.Xpath,"//*[#data-testid='retweet'])) (and in the second EC.element_to_be_clickable) to go to the next tweet for each iteration but I don't know what.
Does anybody know a way to iterate through all my tweets with this method (or another)? I have been thinking about getting the absolute path of all my element in "rt" but I don't know if I can do this using only Selenium. I could also use Twitter API but I want to be able to create a bot on others websites.
Thank you
Ok, I think I have found a solution to my issue.
For each click, I try to catch the exception "ElementClickInterceptedException" when it happens; otherwise I click on my retweet.
So it gaves something like that:
for i in rt:
first_clique=False
while first_clique==False:
try:
i.click()
except ElementClickInterceptedException:
pass
else:
first_clique= True
I'm using Selenium to automate a process and I am using ChroPath to find Elements like XPath for easier use. But sometimes the elements are not available; example:
Element that I'm trying to locate
ChroPath
try:
P_Captcha_ButtonWait = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH("//div[#class='antigate_solver recaptcha in_process']"))))
print("Captcha is being solved. Waiting 40 seconds...")
time.sleep(40)
CaptchaSolved = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[#class='antigate_solver recaptcha solved']")))
time.sleep(3)
print("It's rocking!!!!")
driver.find_element_by_xpath(
"/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/p[3]/button[1]").click()
except:
P_Email_Verification = WebDriverWait(driver, 10).until(EC.presence_of_element_located((
By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[2]/label[1]/div[1]/input[1]")))
driver.find_element_by_xpath(
"/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[2]/label[1]/div[1]/input[1]").click()
print("Something went wrong and idk why?")
This is the line of Code I have written for the Captcha part and I am actually using Anti-Captcha.com Extension and I want to locate the text that Anti-Captcha gives when it is solving the captcha. It looks like this: Anti-Captcha
Also, the website I'm trying this on is ProtonMail, the page where you verify your self if you're a human or not.
I have already coded the part where you verify your self using E-Mail, but I want Captcha as a main-use and the E-Mail Verification as a backhand in case things go wrong with Captcha.
I have tried researching for locating the element, but the only thing I could find is with an image searcher?
I have been forced to use PyAutoGui with absolute directions most of the time for some elements like Button clicking or Clicking in certain fields with absolute direction because I couldn't properly find the XPath.
I'd like to know some ways experience users locate elements.
Update:
This is another attempt of me trying to fix the code and trying different methods but still not working.
try:
P_Captcha_ButtonWait = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//a[#class='status'][contains(.,'Solving is in process...')]")))
if P_Captcha_ButtonWait is True:
print("Captcha is being solved. Waiting 40 seconds...")
time.sleep(40)
CaptchaSolved = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, "//div[#class='antigate_solver recaptcha solved'][contains(.,'Solved')]")))
if CaptchaSolved is True:
time.sleep(3)
print("It's rocking!!!!")
driver.find_element_by_xpath(
"/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/p[3]/button[1]").click()
except TimeoutException:
# P_Email_Verification = WebDriverWait(driver, 10).until(EC.presence_of_element_located((
# By.XPATH, "/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[2]/label[1]/div[1]/input[1]")))
# driver.find_element_by_xpath(
# "/html[1]/body[1]/div[2]/div[1]/div[1]/div[1]/form[1]/div[1]/div[1]/div[2]/label[1]/div[1]/input[1]").click()
print("Something went wrong and idk why?")
driver.close()
I have made the lines in except as comments for now as my main goal is to fix finding the Anti-Captcha Element.
Basically whatever I do, try won't run because the Element isn't founded.
I thought to make it work in another way; if Captcha option is available on selection, driver can wait 30-80 seconds until Captcha is solved. (that's how long anti-captcha takes approx most of the times to solve a captcha). But sometimes they take up to 200 seconds so that will run into errors and 'cause the whole script failing. So that's the reason why I want to locate this element, because it's easier and keeps the script running.
I'm trying to make an If/Else condition on the title of a specific webpage.
If the title is not 'You have been blocked', my script continue.
I've got an issue with selenium in python with different environment, in the first one (OSX) everything works.
I can grab the title check if it contains specific string and continue or not my script.
In Debian and headless mode activated, I can't access to the title of the web page.
driver.get(url)
print(driver.title)
if driver.title != 'You have been blocked':
print('Ok have fun')
else:
print('blocked')
It seems this function .title is very unstable, is there a better way to achieve it ?
Thanks
I've found the right way to work with .title.
Now my script wait the title of the webpage and and I parse it when it is loaded.
wait = WebDriverWait(driver, 15)
wait.until(EC.presence_of_element_located((By.TAG_NAME,"title")))
if (driver.title != 'You have been blocked'):
print('Ok Have Fun')
else:
print('Blocked')
Thanks
At work we have a task which basically requires the following steps:
1. Open a URL
2. Click past an SSL certificate
3. Click a link
4. Repeat 20 times
I've worked out how to automate this task in Python using Selenium, however the code reads like this (This is just the start of over 200 lines):
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
capabilities = webdriver.DesiredCapabilities()INTERNETEXPLORER
capabilities['acceptSslCerts'] = True
driver = webdriver.Ie("C:\\Python27\\IEDriver\\IEDriver.exe")
driver.get("https://example.com")
sleep(5)
driver.find_element_by_link_text("Continue to this website (not recommended).").click()
driver.find_element_by_link_text("Clear cache").click()
driver.close()
sleep(20)
driver = webdriver.Ie("C:\\Python27\\IEDriver\\IEDriver.exe")
driver.get("https://example.com2")
sleep(5)
driver.find_element_by_link_text("Continue to this website (not recommended).").click()
driver.find_element_by_link_text("Clear cache").click()
driver.close()
sleep(20)
And it basically repeats as many times as is required to complete the cache clear.
I'm sure there is a way to loop these actions.. Like creating a .txt document and having Python read from it and use its contents as an argument in the driver.get() part. But I have no idea how to do this.
So far I've worked out that I could define a function that repeats until the list is exhausted.. But I have no idea how to repeat the function while using different arguments for the driver.get() command.
Is this feasible? I'm not really from a programming background and all of this is extremely new to me so I'm still getting a hang of everything..
driver = webdriver.Ie("C:\\Python27\\IEDriver\\IEDriver.exe")
for url in ["https://example.com1",
"https://example.com2",
"https://example.com3",]:
driver.get(url)
sleep(5)
driver.find_element_by_link_text("Continue to this website (not recommended).").click()
driver.find_element_by_link_text("Clear cache").click()
sleep(20)
driver.close()
Go through some python tutorials.