Python Selenium Compose email fail - python

import time
import unittest
from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
#from setuptools.py31compat import unittest_main
username = "robertredrain#gmail.com"
password = ""
tomailid = "robertredrain#yahoo.com"
emailsubject = "robertredrain#yahoo.com"
mailbody = "Great! you sent email:-)" + "\n" + "Regards," + "\n" + "Robert"
class send_email(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.baseUrl = "http://mail.google.com/intl/en/mail/help/about.html"
def test_Login_Email(self):
driver = self.driver
driver.get(self.baseUrl)
driver.maximize_window()
driver.find_element_by_id("gmail-sign-in").click()
driver.find_element_by_id("Email").clear()
driver.find_element_by_id("Email").send_keys(username)
driver.find_element_by_id("next").click()
time.sleep(5)
driver.find_element_by_id("Passwd").clear()
driver.find_element_by_id("Passwd").send_keys(password)
driver.find_element_by_id("signIn").click();
#Verify login
if "Gmail" in driver.title:
print("Logged in sucessfully !!!" + driver.title)
else:
print("Unable to loggin :-( " + driver.title)
time.sleep(5)
def test_Compose_Email(self):
time.sleep(5)
driver = self.driver
driver.find_element_by_xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/div/div").click()
#time.sleep(5)
driver.find_element_by_class_name("vO").send_keys(tomailid)
driver.find_element_by_class_name("aoT").send_keys(emailsubject)
driver.find_element_by_class_name("Am").clear()
driver.find_element_by_class_name("Am").send_keys(mailbody)
driver.find_element_by_xpath("//div[text()='Send']").click()
def tearDown(self):
self.driver.close();
if __name__ == '__main__':
unittest.main()
I try to do "COMPOSE email", but got the error "raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":"/html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/div/div"}"
Could someone help? Thanks a lot!

Make it simple and locate the "Compose" button by text:
//div[. = "COMPOSE"]

Related

Selenium not finding information(worked before)

I have this script to get the bids of an NFT as they come in, It worked perfectly before however all of a sudden it stopped being able to find the information that i needed.
It was able to save the information into a file and then post the message onto discord
however now it cant get there due to it not being able to get the information.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
import asyncio,calendar,discord
import time
from discord.ext import commands
TOKEN = "token here"
url = "https://auction.chimpers.xyz/"
class_names = ["m-bid-amount","m-bid-bidder","m-bid-timestamp","m-countdown-pending","m-name",'img']
bot = bot = commands.Bot(command_prefix="!")
channel_id = channel_id here
async def message_on_bid(auction_status="New Bid Placed",bid="has recieved a bid of",bought="from"):
await bot.wait_until_ready()
channel = bot.get_channel(channel_id)
with open("CurrentTopBidder.txt","r") as f:
info = [line.rstrip() for line in f.readlines()]
if len(info[1]) > 10:
info[1] = info[1][:-(len(info[1])-4):] + "..." + info[1][len(info[1])-4::]
myEmbed = discord.Embed(title=auction_status,url="https://auction.chimpers.xyz/",description=f"{info[4]} {bid} Ξ{info[0][:-4]} {bought} {info[1]}",color=0x202020)
myEmbed.set_footer(text=f"{info[2]}")
myEmbed.set_thumbnail(url=info[5])
await channel.send(embed=myEmbed)
async def main():
start_time = time.time()
while True:
if time.time() - start_time > 60:
await message_on_bid(auction_status="Auction Closed",bid="has been bought for",bought="by")
print("auction closed")
with open("CurrentTopBidder.txt","w") as f:
f.write("0 ETH\nOwner\nTime\nAuctionStatus\nName\nimage")
f.close()
break
driver_options = Options()
driver_options.add_argument("--headless")
driver = webdriver.Chrome("Drivers\Chromedriver.exe")
driver.get(url)
results = {}
try:
for class_name in class_names:
if class_name != "img":
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, class_name)))
# Getting inner text of the html tag
results[class_name] = element.get_attribute("textContent")
else:
element = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.TAG_NAME,class_name)))
results[class_name] = element.get_attribute("src")
finally:
driver.quit()
print(results)
this_list = []
f = open("CurrentTopBidder.txt","r")
for line in f.readlines():
this_list.append(line.rstrip())
if results["m-name"] == "Unnamed":
results["m-name"] = this_list[4]
if float(results["m-bid-amount"][:-4:]) > float(this_list[0][:-4:]):
if results["m-countdown-pending"] == " Auction ended ":
status = "AuctionEnded"
else:
status = "CurrentTopBidder"
f = open("CurrentTopBidder.txt","w")
f.write(str(results["m-bid-amount"]) + "\n" + str(results["m-bid-bidder"] + "\n" + str(results["m-bid-timestamp"]) + "\n" + str(status) + "\n" + str(results["m-name"]) + "\n" + results["img"]))
f.close()
await message_on_bid()
else:
f.close()
driver.quit()
await asyncio.sleep(600)
if __name__ == "__main__":
bot.loop.create_task(main())
bot.run(TOKEN)
any reason as to why this stopped being able to find the information????
error here

Selenium save file to current working directory

I have a website which I'm querying after solving a CAPTCHA.
After solving the CAPTCHA my query downloads a PDF file. My issue is that I cannot get FireFox to download the file automatically to the current working directory without user interaction.
I also cannot figure out how to determine if the file already exists, which would prompt my code to display either a dialog or a message.
Here's my current code, which does everything correctly until the file download popup.
import os
import logging
import argparse
import requests
from time import sleep
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
logger = logging.getLogger('tst-log-query')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)-5.5s - %(message)s', "%Y-%m-%d %H:%M:%S")
file_handler = logging.FileHandler(
'tst-log-query.log', 'w', encoding='utf-8')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
mainurl = "https://cndt-certidao.tst.jus.br/inicio.faces"
ckey = "f1a382ddd51949057324a7fc7c8ccf8a"
def solver(captcha):
with requests.Session() as req:
print("[*] - Please wait while CAPTCHA is solved ")
cdata1 = {
"clientKey": ckey,
"task": {
"type": "ImageToTextTask",
"body": captcha
}
}
cdata2 = {
"clientKey": ckey
}
while True:
try:
r = req.post(
'https://api.anti-captcha.com/createTask', json=cdata1)
cdata2['taskId'] = r.json()['taskId']
break
except KeyError:
logger.debug(r.json()["errorDescription"])
continue
while True:
sleep(5)
logger.info("Slept 5 Seconds!")
fr = req.post(
'https://api.anti-captcha.com/getTaskResult', json=cdata2)
status = fr.json()
logger.debug("Status: {}".format(status["status"]))
if status['status'] == "ready":
cap = status['solution']['text']
print("[*] - CAPTCHA Solved!")
return cap
else:
continue
def main(pat):
# saving to current working directory
options = Options()
options.set_preference('browser.download.folderList', 2)
options.set_preference('browser.download.manager.showWhenStarting', False)
options.set_preference('browser.download.dir', os.getcwd())
options.set_preference(
'browser.helperApps.neverAsk.saveToDisk', 'pdf')
#__________________________#
driver = webdriver.Firefox(options=options)
print(f"Checking (CNPJ/CPF)# {pat}")
while True:
try:
driver.get(mainurl)
waiter = WebDriverWait(driver, 60)
waiter.until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "input[value=Regularização]"))
).click()
waiter.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "#consultarRegularizacaoForm\:cpfCnpj"))
).send_keys(pat)
cap = waiter.until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "img[src^=data]"))).get_attribute('src').split(',', 1)[1]
break
except exceptions.TimeoutException:
logger.error('[*] - Unable to found elements, Refreshing Request.')
continue
capso = solver(cap)
if capso:
driver.find_element(By.ID, 'idCaptcha').send_keys(capso)
driver.find_element(
By.ID, 'consultarRegularizacaoForm:btnEmitirCertidao').click()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Download PDF File!')
parser.add_argument(
'pattern', metavar="(CNPJ/CPF) Number", help="(CNPJ/CPF) Number", type=str)
try:
main(parser.parse_args().pattern)
except KeyboardInterrupt:
exit("Good Bye!")
Usage: python script.py 15436940000103
options = Options()
options.headless = True
options.set_preference(
"browser.helperApps.neverAsk.saveToDisk", "application/pdf")
options.set_preference("browser.download.folderList", 2)
options.set_preference("browser.download.dir", os.getcwd())
options.set_preference("pdfjs.disabled", True)
driver = webdriver.Firefox(options=options)
Solved using the previous code.

Python selenium Element not clickable since another element obscures it

I have the following code. There is a problem in the line with:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
It raises this exception:
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <input id="tp1594226550595" class="time hasTimepicker" type="text"> is not clickable at point (753,293) because another element <rect class="highcharts-background"> obscures it
I have already tried solutions which use 'wait', but there is a timeout exception. It seems the obstruction is permanent. I have also tried ActionChains, but it's not working either.
The element I am trying to click on is in this image:
The graph is the element obscuring the time picker at the top:
from time import sleep, time
from selenium import webdriver
from selenium.common.exceptions import \
NoSuchElementException, \
ElementClickInterceptedException, \
ElementNotInteractableException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.download.dir", "C:/Users/USER/Documents/temp")
fp.set_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")
print('opening browser')
browser = webdriver.Firefox(
executable_path='C:/Users/USER/Documents/Python Workspace/geckodriver/geckodriver.exe',
firefox_profile=fp
)
print('opening link')
browser.get('https://webtrader.binary.com/v2.2.8/main.html#historical-data')
def close_popup():
entrytime = time()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
close1 = browser.find_element_by_xpath('/html/body/div[3]/div[1]/div/button/span[1]')
except NoSuchElementException:
sleep(2)
close_popup()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
click_element(close1)
except UnboundLocalError:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
close_popup()
def click_element(element):
entrytime = time()
try:
currenttime = time()
if currenttime - entrytime >= 10:
return
element.click()
sleep(2)
except ElementClickInterceptedException:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
click_element(element)
except ElementNotInteractableException:
currenttime = time()
if currenttime - entrytime >= 10:
return
sleep(2)
click_element(element)
def resources():
try:
global res
res = browser.find_element_by_link_text('Resources')
res.click()
sleep(2)
except NoSuchElementException:
resources()
print('accessing Resources')
resources()
print()
#
#
def historical_data():
global res
try:
hd = browser.find_element_by_link_text('Historical Data')
sleep(2)
except ElementNotInteractableException:
sleep(2)
historical_data()
except NoSuchElementException:
sleep(2)
res.click()
historical_data()
try:
hd.click()
sleep(2)
except UnboundLocalError:
sleep(2)
historical_data()
# hd.click()
# sleep(2)
print('accessing Historical data')
historical_data()
maxWindow = browser.find_element_by_xpath('/html/body/div[8]/div[1]/div/a[3]/span')
browser.execute_script('arguments[0].click();', maxWindow)
sleep(2)
print('setting data type to 1 Tick')
while True:
try:
dt = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]')
break
except NoSuchElementException:
sleep(2)
dt.click()
sleep(2)
tick = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span')
tick.click()
sleep(2)
#
while True:
try:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
Try this instead:
while True:
try:
timePicker = browser.find_element_by_xpath('/html/body/div[8]/div[4]/input[2]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
Additionally, you import WebDriverWait, but don't use it. Try this, replace:
while True:
try:
dt = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]')
break
except NoSuchElementException:
sleep(2)
dt.click()
sleep(2)
tick = browser.find_element_by_xpath('/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span')
tick.click()
sleep(2)
#
while True:
try:
timePicker = browser.find_element_by_xpath('//*[#class="time hasTimepicker"]')
break
except NoSuchElementException:
sleep(2)
timePicker.click()
with this:
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[1]/span/span[1]'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[2]/div/div[1]/div[1]/div/div[1]/div/div[1]/div[2]/div[1]/div[2]/span'))).click()
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[8]/div[4]/input[2]'))).click()
You'll immediately notice its significantly faster, and cleaner. I'd replace all those while,try, and time.sleep() with WebDriverWait.

Chrome user:pass login not working?

I am attempting to use chrome with a user pass proxy this is my code:
def getProxy(string_only=True):
try:
proxy_manager = ProxyManager("proxies.txt")
proxy = proxy_manager.random_proxy()
if string_only:
return proxy.proxy_string
return proxy.get_dict()
except (OSError, IOError, IndexError) as e: # couldn't load the file / file is empty
return None
chrome_options = webdriver.ChromeOptions()
proxy = getProxy()
userpass = proxy[0:proxy.find("#")]
hostport = proxy[proxy.find("#")+1:]
if proxy:
chrome_options.add_argument("--proxy-server=" + hostport)
driver1 = webdriver.Chrome(chrome_options=chrome_options)
wait = WebDriverWait(driver1, 1000000)
if manual == "No":
driver1.get("http://%s#www.google.com"%userpass)
Help is appreciated

Selenium Web-driver automation not working properly

So I found this script online, which is meant to brute-force web-forms online etc. using Selenium, and I thought it would be a good idea to take it, modify it a bit and experiment with it. This time, I tried creating a bot that:
Signs up to Twitter
Goes to twitter.com
Posts something.
Logs out.
Loop 1, 2, 3, 4 again
However, when I run the script, it just pops up a browser window and does nothing. Then the Terminal ends the Python script like it did its work correctly and finished with no problems...
Code (note that the script might look weird for what I want, but that's because I found the script as a web-form brute-forcer online, and decided to modify it to my needs):
#!/bin/python
from mainLib import *
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import simplejson as json
import sys
import optparse
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36")
driver = "reserved"
def userExists(username):
try:
driver.get("https://twitter.com/"+username)
assert (("??" or "?Twitter / ?") not in driver.title)
except AssertionError:
print '\033[1;31mUser\033[1;m',
print '\033[1;35m#%s\033[1;m' %username,
print '\033[1;31mdoes not exist.\033[1;m',
print '\033[1;33mTrying with the next Username...\033[1;m'
print ' '
return 1
except:
'uknown error'
def login(user, password, delay):
try:
print '\033[1;33mCreating account with mail: \033[1;m' + '\033[1;35m' + password + '\033[1;m' '\033[1;33m ...\033[1;m'
sleep(2)
if driver.current_url == 'https://twitter.com':
print '\033[1;33mPlease retry using a different IP Address (Proxy/VPN).\033[1;m'
driver.get("https://twitter.com/signup")
if driver.title == "Login on Twitter":
driver.get("https://twitter.com/signup")
sleep(3)
elem = driver.find_element_by_id('full-name')
elem.clear()
elem.send_keys('john')
elem = driver.find_element_by_id('email')
elem.clear()
elem.send_keys(password)
elem = driver.find_element_by_id('password')
elem.clear()
elem.send_keys("twitter1")
sleep(3)
elem.send_keys(Keys.RETURN)
sleep(delay + 3)
if driver.title == "Twitter / Error":
print ' \033[1;31mFailed!\033[1;m'
driver.get("https://twitter.com/signup")
sleep(3)
if driver.title == "Login to Twitter":
print ' \033[1;31mFailed!\033[1;m'
driver.get("https://twitter.com/signup")
sleep(3)
# if "This email is already registered." in driver.page_source:
# print ' \033[1;31mFailed!\033[1;m'
if driver.current_url == 'https://twitter.com/account/access':
print ' \033[1;31mFailed!\033[1;m'
print ("")
print '\033[1;33mPlease retry using a different IP Address (Proxy/VPN).\033[1;m'
driver.close()
sys.exit("")
assert (("Enter your phone") not in driver.title)
except AssertionError:
print ' \033[1;32mSuccess!\033[1;m'
# print '\033[1;35mEmail: \033[1;m' + password
# print '\033[1;35mPassword: \033[1;m' + "twitter1"
# print("")
try:
f = open('CreatedAccounts.txt','a')
except:
f = open('CreatedAccounts.txt','w')
f.write(password+'\n')
f.close()
driver.get("https://twitter.com")
elem = driver.find_element_by_id('tweet-box-home-timeline')
elem.clear()
elem.send_keys('It worked!')
elem = driver.find_element_by_xpath('//*[#id="timeline"]/div[2]/div/form/div[2]/div[2]/button')
elem.send_keys(Keys.RETURN)
time.sleep(5)
driver.get("https://twitter.com/logout")
sleep(5)
elem = driver.find_element_by_css_selector("button.js-submit").click()
sleep(5)
driver.get("https://twitter.com/signup")
# driver.delete_all_cookies()
# return 1
# else:
# print '\033[1;33mPlease check your Internet Connection.\033[1;m'
def dictionaryAttack(usernames,passwords,delay):
if str(type(usernames)) == "<type 'list'>":
for username in usernames:
#if (userExists(username) == 1):
# continue
driver.get("https://twitter.com/signup")
sleep(delay)
print("Creating Accounts...")
print("")
for password in passwords:
if (login(username,password,delay) == 1):
cj.clear()
break
def main():
parser = optparse.OptionParser()
parser.add_option('-f', '--file', action="store", dest="userfile", help="File containing valid usernames (one per line)", default=False)
parser.add_option('-d', '--dictionary', action="store", dest="dictionary", help="Text file containing passwords", default=False)
parser.add_option('-u', '--username', action="store", dest="username", help="A valid username", default=False)
parser.add_option('-t', '--time', action="store", dest="delay", help="Delay (in seconds) - use this option based on your Network Connection speed.", default=True)
options, args = parser.parse_args()
global driver
if (options.delay is None):
delay = 4
else:
delay = int(options.delay)
print '\033[1;33mUsing\033[1;m',
print '\033[1;35m%d second(s)\033[1;m' %delay,
print '\033[1;33mof delay between login attempts.\033[1;m'
print ' '
if ( (options.userfile == False) and (options.username == False) ) :
print 'You have to set an username or a userfile'
exit()
if ( (options.userfile != False) and (options.username != False) ) :
print 'You can\'t set both options at once.. choose between username or userfile'
exit()
if (options.dictionary == False):
print 'You have to set a valid path for the passwords dictionary.'
exit()
try:
f = open(options.dictionary,'r')
passwords = []
while True:
line = f.readline()
if not line:
break
passwords.append(line.strip('\n'))
f.close()
except:
print 'Check the path to the dictionary and try again.'
exit()
if (options.userfile != False):
try:
f = open(options.userfile,'r')
usernames = []
while True:
line = f.readline()
if not line:
break
usernames.append(line.strip('\n'))
f.close()
except:
print 'Check the path to the users file and try again.'
exit()
driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
dictionaryAttack(usernames,passwords,delay)
else:
driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
dictionaryAttack(options.username,passwords,delay)
driver.close()
if __name__ == '__main__':
main()
See the link below. That could be the problem.
https://github.com/SeleniumHQ/selenium/issues/2257

Categories

Resources