I posted recently about some trouble I was having with selenium, primarily the anticaptcha API. Ive managed to solve that but I am having some trouble over here. This is my current code:
from time import sleep
from selenium import webdriver
from python_anticaptcha import AnticaptchaClient, NoCaptchaTaskProxylessTask
import os
import time
#Gather Api Key
api_key = 'INSERT API KEY HERE'
#Go to the acc registration site
browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get('https://www.reddit.com/register/')
sleep(2)
#Input email
email_input = browser.find_element_by_css_selector("input[name='email']")
email_input.send_keys("INSERT EMAIL HERE")
#Continue to the next part of the registration process
continue_button = browser.find_element_by_xpath("//button[#type='submit']")
continue_button.click()
#Find and input the username and password fields
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("INSERT USERNAME HERE")
password_input.send_keys("INSERT PASSWORD HERE")
#Gather site key
url = browser.current_url
site_key = "6LeTnxkTAAAAAN9QEuDZRpn90WwKk_R1TRW_g-JC"
#Acc do the solving process
client = AnticaptchaClient(api_key)
task = NoCaptchaTaskProxylessTask(url, site_key)
job = client.createTask(task)
print("Waiting for recaptcha solution")
job.join()
# Receive response
response = job.get_solution_response()
print(response)
print("Solution has been gotted")
# Inject response in webpage
browser.execute_script('document.getElementById("g-recaptcha-response").innerHTML = "%s"' % (response))
print("Injecting Solution")
# Wait a moment to execute the script (just in case).
time.sleep(1)
print("Solution has been gotted for sure")
# Press submit button
browser.implicitly_wait(10)
Signup = browser.find_element_by_xpath('//input[#type="submit"]')
Signup.click()
Everything runs smoothly except for the final line. I think the program is recognizing the submit button but for some reason gives an element not interactable error. Any help on how to solve this would be greatly appreciated
I had the same issue when I was using selenium. Sometimes it happens that even though selenium has recognized the element, its function is not "ready." Adding a delay before clicking the submit button should fix the issue.
Related
I have an Instagram robot class that takes the username and password and logs in to the account.
I wrote a comment function in this class to put the comment below the post, I use this function but when it opens a custom post, when I leave a comment I get an error.
I do not know the reason for this error
Please let me know if anyone knows a solution
my code:
from selenium import webdriver
import time
import random
class InstaBot:
#Create a contractor to open the browser and get the username and password of your Instagram account
def __init__(self,username,password):
self.driver = webdriver.Firefox()
self.username = username
self.password = password
#This function is used to login to your Instagram account
def login(self):
driver = self.driver
#Open the browser and open the Instagram site
driver.get('https://www.instagram.com')
#Find the username box address via XPath
user_box = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
#Find the password box address via XPath
password_box = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
#Find the login button via Xpath
button = driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]')
#Click on the username box
user_box.click()
#And enter the username in the username box
user_box.send_keys(self.username)
time.sleep(5)
#Click on the password box
password_box.click()
#And enter the password in the password box
password_box.send_keys(self.password)
time.sleep(5)
#Finally, click the Login button
button.click()
time.sleep(5)
#Go to the home page by adding a username to continue the Instagram link
acount = driver.get('https://www.instagram.com/%s/'%(self.username))
def comment(self,message=None):
driver = self.driver
driver.get('https://www.instagram.com/p/CLP-HN9AP0i/')
coment_box = driver.find_element_by_xpath('/html/body/div[1]/section/main/div/div[1]/article/div[3]/section[3]/div/form/textarea').click()
coment_box.send_keys(message)
mobin = InstaBot('your user name','your password')
mobin.login()
time.sleep(5)
mobin.comment('Very Good')
The error it gives me:
Traceback (most recent call last):
File "C:\Users\Mobin\Desktop\test_projcet.py", line 50, in <module>
mobin.comment('Very Good')
File "C:\Users\Mobin\Desktop\test_projcet.py", line 45, in comment
coment_box.send_keys(message)
AttributeError: 'NoneType' object has no attribute 'send_keys'
Problem
The problem it you are setting your comment_box equal to an driver action not a web element. It will send out error "object has no attribute 'send_keys'" because it not an web element.
Also your comment_box is load more than one time. So you need to declare comment_box again after clicking it the first time.
Solution
remove click at the end of this line
coment_box = driver.find_element_by_xpath('your xpath').click()
replace with this code, it should work:
coment_box = driver.find_element_by_xpath('your xpath')
coment_box.click()
time.sleep(5)
coment_box_2 = driver.find_element_by_xpath('your xpath')
coment_box.send_keys(message)
I am developing a website that works with Asana, so when a new user registers on my website, they should also be automatically registered on Asana as well.
How can I use python to register a new account on Asana based on the email and password provided on the sign up page of my site?
https://asana.com/
I found the script someone posted for sign up facebook, can it be used to sign up on Asana? "usr" "pwd" should be the input on my website.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
usr=input('Enter Email Id:')
pwd=input('Enter Password:')
driver = webdriver.Chrome()
driver.get('https://www.facebook.com/')
print ("Opened facebook...")
sleep(1)
a = driver.find_element_by_id('email')
a.send_keys(usr)
print ("Email Id entered...")
sleep(1)
b = driver.find_element_by_id('pass')
b.send_keys(pwd)
print ("Password entered...")
c = driver.find_element_by_id('loginbutton')
c.click()
print ("Done...")
sleep(10)
driver.quit()
Has anyone faced the problem that automation routine for instagram written in python with selenium chromedriver has become difficult to run recently because instagram keeps asking for the code it sends in sms or email?
When you do your normal browser login it asks for the code in sms only once. But when you do it with selenium it asks for it every time.
Here is the code
options = webdriver.ChromeOptions()
#options.add_argument('headless')
#options.add_argument('--headless')
options.add_argument('--disable-logging')
options.add_argument('--log-level=3')
driver = webdriver.Chrome(chrome_options=options)
#driver = webdriver.Chrome()
print('Driver started successfully!')
driver.get("https://instagram.com/")
time.sleep(6)
pg=driver.find_element_by_tag_name("html")
lng=pg.get_attribute("lang")
#print(lng)
if lng=='en':
global lin
global foll
global foll_tx
global subscr_tx
lin="Log in"
foll="followers"
foll_tx="Follow"
subscr_tx="following"
get_enter_bt
= driver.find_elements_by_link_text(self.lin)
lin_found=False
while not lin_found:
if len(get_enter_bt)==0:
print('Login not found ((( Refreshing...')
driver.refresh()
time.sleep(6)
get_enter_bt = driver.find_elements_by_link_text(self.lin)
else:
lin_found=True
print('Login button found!')
time.sleep(3)
get_enter_bt[0].click()
time.sleep(3)
#login
login = driver.find_element_by_name("username")
login.send_keys(username)
login = driver.find_element_by_name("password")
login.send_keys(password)
login.send_keys(Keys.RETURN)
time.sleep(9)
get_close_mobapp=driver.find_elements_by_css_selector("button._dbnr9")
if len(get_close_mobapp)!=0:
get_close_mobapp[0].click()
notif_switch=driver.find_elements_by_css_selector("button.aOOlW.HoLwm")
print('notif butt %s' % len(notif_switch))
if len(notif_switch)>0:
notif_switch[0].click()
print(1)
#detect suspicious login
susp_login_msg=driver.find_element_by_xpath("//*[#id=\"react-root\"]/section/div/div/div[1]/div/p")#<p class="O4QwN">Подозрительная попытка входа</p>
print('susm login msg %s' % (susp_login_msg!=None))
if susp_login_msg:
if susp_login_msg.text=='Подозрительная попытка входа':
try:
mobile_button = driver.find_element_by_xpath("//*[#id=\"react-root\"]/section/div/div/div[3]/form/div/div[2]/label")
mobile_button.click()
except:
mobile_button = driver.find_element_by_xpath("//*[#id=\"react-root\"]/section/div/div/div[3]/form/div/div[1]/label")
mobile_button.click()
snd_code_btn=driver.find_element_by_xpath("//*[#id=\"react-root\"]/section/div/div/div[3]/form/span/button")
snd_code_btn.click()
print('Instagram detected an unusual login attempt')
print('A security code was sent to your mobile '+mobile_button.text)
security_code = input('Type the security code here: ')
#security_code_field = driver.find_element_by_xpath(("//input[#id='security_code']"))
security_code_field.send_keys(security_code)
‐----‐‐‐----
This code works fine, but how to stop instagram from asking for the code in sms every time? Does it detect that I run selenium and runs a kind of antibot activity?
I was running the script on schedule to perform series of likes for my subscribers for example which is time consuming you know and automation was my remedy)))
So i'm using this 2captcha API and testing it on a site like omegle.com.
The captcha solving happens but the google captcha box doesnt get ticked and nothing happens. Wondering why that is, I know the 2captcha API runs perfectly... but does it only work for HTTP requests and not selenium?
Here is the API link i inserted into the code below:
https://github.com/2captcha/2captcha-api-examples/blob/master/ReCaptcha%20v2%20API%20Examples/Python%20Example/2captcha_python_api_example.py
from selenium import webdriver
from time import sleep
from selenium.common.exceptions import InvalidElementStateException
from selenium.common.exceptions import UnexpectedAlertPresentException
import time,os
import requests
fp = webdriver.FirefoxProfile('C:\\Users\\mo\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\b0wnbtro.dev-edition-default')
interest = input("Enter the interests seperate by a comma ")
msg1 = "1"
msg2 ="2"
msg3 = "3"
msg4 = "4"
driver = webdriver.Firefox(fp)
#2CAPTCHA API CODE INSERTED HERE FOR A TEST RUN BEFORE BEING INCORPORATED IN A LOOP
def main():
try:
driver.get('http://www.omegle.com')
time.sleep(1)
#driver.find_elements_by_xpath("//*[contains(text(), 'I'm not a robot')]")
#send.click()
driver.find_element_by_xpath('//textarea[#rows="3"]').clear()
message = driver.find_element_by_xpath('//textarea[#rows="3"]')
time.sleep(3)
message.send_keys(msg1)
send = driver.find_element_by_xpath('//button[#class="sendbtn"]')
send.click()
time.sleep(6)
message.send_keys(msg2)
send = driver.find_element_by_xpath('//button[#class="sendbtn"]')
send.click()
time.sleep(10)
message.send_keys(msg3)
send = driver.find_element_by_xpath('//button[#class="sendbtn"]')
send.click()
time.sleep(25)
message.send_keys(msg4)
send = driver.find_element_by_xpath('//button[#class="sendbtn"]')
send.click()
disconnect = driver.find_element_by_xpath('//button[#class="disconnectbtn"]')
disconnect.click()
disconnect = driver.find_element_by_xpath('//button[#class="disconnectbtn"]')
disconnect.click()
disconnect = driver.find_element_by_xpath('//button[#class="disconnectbtn"]')
disconnect.click()
except (InvalidElementStateException, UnexpectedAlertPresentException):
main2()
def main2():
try:
driver.get('http://www.omegle.com')
interest1 = driver.find_element_by_xpath('//input[#class="newtopicinput"]')
interest1.send_keys(interest)
btn = driver.find_element_by_id("textbtn")
btn.click()
time.sleep(5)
driver.find_element_by_xpath('//textarea[#rows="3"]').clear()
message = driver.find_element_by_xpath('//textarea[#rows="3"]')
time.sleep(1)
time.sleep(2)
message.send_keys(msg1)
send = driver.find_element_by_xpath('//button[#class="sendbtn"]')
send.click()
time.sleep(6)
message.send_keys(msg2)
send.click()
time.sleep(10)
message.send_keys(msg3)
send.click()
time.sleep(25)
message.send_keys(msg4)
send.click()
send.click()
disconnect = driver.find_element_by_xpath('//button[#class="disconnectbtn"]')
disconnect.click()
except (InvalidElementStateException,UnexpectedAlertPresentException) :
disconnect = driver.find_element_by_xpath('//button[#class="disconnectbtn"]')
disconnect.click()
else:
main2()
while True:
try:
main2()
except (InvalidElementStateException,UnexpectedAlertPresentException) :
main()
I hope you already found a solution, but want to leave a comment for those who can get stuck at the same point.
The API does work for Selenium too.
The checkbox will not be ticked, it is controlled by ReCaptcha javascript and you do not touch it.
All you need to do is to place the token into g-recaptcha-response field. With Selenium you can do that executing JavaScript
document.querySelector('#g-recaptcha-response').textContent='token_string'
And in your case as there's nothing that submits the form you have to execute the callback function that is JavaScript too. For example:
___grecaptcha_cfg.clients[0].NY.N.callback('token_string')
The path of callback function changes so you need to find a valid one exploring ___grecaptcha_cfg object.
I want to make a python program that gets all links for a certain Google search query so I loop over the 30 search pages and when it gives me a ReCaptcha I do it manually
here is how my code looks like :
driver = webdriver.Firefox()
number_pages = 30
query = 'hello world'
query = urllib.parse.quote_plus(query)
url = "https://www.google.com/search?q="+query+"&&start="
with open('result.txt','w') as fp:
for i in range(1,number_pages-1):
# loop over the 30 pages
page_url = url + str((i-1)*10)
print("# " + page_url)
driver.get(page_url)
while len(driver.find_elements_by_id('recaptcha')) != 0:
# ReCaptcha , sleeping until the user solve the recaptcha
print('sleeping...!')
time.sleep(10)
els = driver.find_elements_by_tag_name('cite')
But when i try to send the recaptcha form it gaves me the error:
Cannot contact reCAPTCHA. Check your connection and try again
and when I use a normal navigator (Google Chrome or Firefox ) the error don't occur
I think the ReCaptcha blocks the webdriver
Please anyone can explain what exact issue here, and how can be fixed.