I am building a stock trading bot for fun and buying/selling on a stock trading simulator. I have all the webscraping done, all the send_keys done. I just want to be able to execute multiple lines of code as one simple command instead of having to repeat code over and over, making the program really long. For example, if I want to buy a stock, I have to execute all this code for it to complete the buy order:
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[1]/div/div[1]/input').click()
driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN)
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/table/tbody/tr/td[2]/a/span').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
time.sleep(1)
driver.find_element_by_xpath('//*[#id="shares"]').click()
driver.find_element_by_xpath('//*[#id="shares"]').clear()
driver.find_element_by_xpath('//*[#id="shares"]').send_keys('0.01')
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[1]/form/div[3]/div/button[3]').click()
Im pretty new at this and I know this wont work but, can I do something like:
Buy = driver.find_element_by_xpath('/html/body/div[4]/div/div[1]/input').click()
driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN)
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[4]/table/tbody/tr/td[2]/a/span').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
time.sleep(1)
driver.find_element_by_xpath('//*[#id="shares"]').click()
driver.find_element_by_xpath('//*[#id="shares"]').clear()
driver.find_element_by_xpath('//*[#id="shares"]').send_keys('0.01')
driver.find_element_by_xpath('/html/body/div[7]/div/div[1]/form/div[3]/div/button[3]').click()
Then I can just add the 'Buy' (or whatever) variable in my If statement instead of that whole list of code.
if xxxxxxxxx
execute "Buy"
You mean like a function?
def buy():
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/div[1]/div/div[1]/input').click()
driver.find_element_by_xpath('/html/body/div[4]/input').send_keys(('GOOGL') , Keys.RETURN)
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[4]/div[3]/div[1]/table/tbody/tr/td[2]/a/span').click()
time.sleep(1)
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[3]/div/button').click()
time.sleep(1)
driver.find_element_by_xpath('//*[#id="shares"]').click()
driver.find_element_by_xpath('//*[#id="shares"]').clear()
driver.find_element_by_xpath('//*[#id="shares"]').send_keys('0.01')
driver.find_element_by_xpath('/html/body/div[7]/div/div/div[1]/form/div[3]/div/button[3]').click()
Now you can run your code just with buy().
if condition:
buy()
def x(): was the correct solution.
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've been trying to work on practice modules in the python crash course book. I'm working on while loops and input functions. I tried running the below code in the terminal but only the second line of code prints.
What rental car would you like?
The full code is below:
while True:
car = input("What rental car would you like?")
print(f'Let me see if I can find you a {car.title()}.')
car += input('Subaru')
break
while True:
table = input("How many people are in your party?")
table = int(table)
if table > 8:
print('My apologies, you will have to wait for a table.')
else:
print('Your table is ready!')
break
I tried breaking the loops apart to get the rest to print in the terminal but I'm not sure where I'm messing up. I don't get any errors, it just only prints one line of code out of the entire thing.
It is waiting for your input. After you write something and hit enter it will proceed.
I did not quite understand the first while loop, but for the second one the problem is with the indentation of the break statement
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 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.
I'm teaching myself python with the aid of Head First Programming.
I thought I might tweak their example more to my liking, but I think I'm missing something about time.sleep.
My code is
print("Welcome to SuperBeans!")
time.sleep(3)
answer = input("Do you need a price now? y/n: ")
But rather than pausing for three seconds after the welcome message, it waits 3 minutes, then displays both the message and the input request. What am I missing?
Additionally, the program seems to hang indefinitely when running the "poll" function I defined, without ever displaying the "polling..." notice. I know get_price() is working because it prints it in another section ...
def poll():
price = 99.99
print(price)
while price > 4.74:
print("Polling...")
price = get_price()
time.sleep(5)
print("Buy!")
So why is the welcome pausing before I think it should be, and why is the poll() function hanging?
As discussed in comments above, Python output is being buffered, so the programs run (and sleeps for the right time etc) but you don't see the output for a while.
If you're running Python from a nonstandard console, you need to pass the "-u" option to Python to disable the buffering. I.e.:
python -u foo.py
About why is poll() function hanging, this may be because it stays in the while loop.
So question: is get_price() ever returning something strictly greater than 4.74 ? (hint: print the value returned)