I'm making a Python program that opens Target and buys an item entered by the user. If the item is unavailable, the program does nothing and prints a message in the console. To check if the item is available, I need to check if the screen includes something similar to a screenshot I took. The screenshot is shown below.
Can anyone help me with this?
Well, you sure can compare two screenshots, using library as matplotlib, PIL or opencv (or even numpy) but I guess the easiest path is to use selenium (if you automate your navigation) or beautifulsoup4/scrapy.
Something like :
from selenium import webdriver
browser = webdriver.Firefox()
url = "https://www.target.com/"
browser.get(url)
# navigate to find your product
out_of_stock_text = "put your stuff here"
if out_of_stock_text in browsers.page_source:
# Do your stuff here
Good luck !
Related
Hey SO community once again, im running into a small problem while trying to automate something, So I'm watching this YouTube tutorial and I know no ones going to go through the hassle to check what the guy is doing for reference at approximately 5:50 he copies the xpath and pastes it on the side, however when I do it it gets underlined in red. here's what I did
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/Users/name/Desktop/Selenium/chromedriver')
url = 'https://healthscreening.schools.nyc/?type=G'
driver.get(url)
//*[#id="guest_last_name"]
//*[#id="guest_email"]
//*[#id="btnDailyScreeningSubmit"]/button
driver.find_element_by_xpath('')...
The /*[#id=""] part is all underlined in red does anyone know the cause of the problem is? Thanks
You didn't complete the video, did you?
The reason you're getting red underlines is because your IDE has detected them as invalid strings and they need to be enclosed in quotes (single quotes as there are double quotes in the string already).
Moreover, if you watch the video a little further, you'd see that he just copied it because he doesn't want to go back to the Inspect screen again to get the XPATHs.
Later, he writes:
driver.find_element_by_xpath('//*[#id="guest_last_name"]')
The code should look something like:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path='/Users/name/Desktop/Selenium/chromedriver')
url = 'https://healthscreening.schools.nyc/?type=G'
driver.get(url)
last_name = driver.find_element_by_xpath('//*[#id="guest_last_name"]')
email = driver.find_element_by_xpath('//*[#id="guest_email"]')
button = driver.find_element_by_xpath('//*[#id="btnDailyScreeningSubmit"]/button')
# later you can click on these elements, pass characters to it with send_keys etc.
One suggestion that would really help while watching tutorials. Don't try to type along as the instructor is typing.
Watch it completely and try to understand the concept. Then do it yourself without watching. You can of course take a peek if you're stuck but the point is to type out the code from what you remember.
Then you could write your own code, automate something on your own. You'd encounter errors, you'll fix it and you'll learn even more.
You're doing great! Good luck on your journey.
This is my third day ever using Python, and I would like to know if there is a way to get user input for opening a URL. Meaning I would like to know how to ask the user what URL they would like to open and run the code provided in my py file. I tried something simplistic like
from selenium import webdriver
driver = webdriver.Chrome()
print('Link?')
webPage = input()
driver.get(webPage)
but it just opens a tab first in PyCharm without asking anything, then fails to open the link the user may have put in. Again I would like to apologize if the code is horrendous, pretty new at this lol. Appreciate any help, thanks!
from selenium import webdriver
browser = webdriver.Chrome(r"chromedriver.exe")
browser_2 = webdriver.Chrome(r"chromedriver.exe")
browser.get("https://www.seleniumhq.org/")
browser_2.get("https://www.seleniumhq.org/")
browser.find_element_by_id("menu_download").click()
browser_2.find_element_by_id("menu_download").click()
Is there a cleaner way to click on the download button in both windows or do I have to repeat the code for each instance of the browser?
I am making a program where I need to insert text, tick boxes ect in several instances of the same website and do not want to have to repeat the same code for each window.
Each browser is a different instance. You can not use a part of the code for both. So the answer is no. You cannot use another to do this. Your above code is correct though.
How about this:
path = 'chromedriver.exe'
n_drivers = 2
drivers = [webdriver.Chrome(path) for i in range(n_drivers)]
for driver in drivers:
driver.get('https://www.seleniumhq.org/')
driver.find_element_by_id('menu_download').click()
What I want to do is to write a Python script to recognize a specific link and make a left-click on it. To be more explanatory, here are two images from websites:
Page 1
and
Page 2
I want the script to click on "enter site" in page 1 and "try again" on page 2. If possible I want to use machine learning having only the screen image input. It is probably easier to go through the site source, but I want to be further challenged. I gave this a thought but couldn't figure out where to start.
Using screen visual only, you can use:
import pyautogui
and then look for the
pyautogui.locateOnScreen() or pyautogui.locateCenterOfScreen() functions.
you can use selenium:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("http://www.youradress.org")#put here the adress of your page
elem = driver.find_elements_by_xpath("//*[#type='submit']")#put here the content you have put in Notepad, ie the XPath
print(elem .get_attribute("class"))
driver.close()
you can get the class of the html element, or its id, and then pass it to the script and do whatever you want.
When I try to automatically download a file from some webpage using Python,
I get Webpage Dialog window (I use IE). The window has two buttons, such as 'Continue' and 'Cancel'. I cannot figure out how to click on the Continue Button. The problem is
that I don't know how to control Webpage Dialog with Python. I tried to use
winGuiAuto to find the controls of the window, but it fails to recognize any Button type
controls... An ideas?
Sasha
A clarification of my question:
My purpose is to download stock data from a certain web site. I need to perform it for many stocks so I need python to do it for me in a repetitive way. This specific site exports the data by letting me download it in Excel file by clicking a link. However after clicking the link I get a Web Page dialog box asking me if I am sure that I want to download this file. This Web page dialog is my problem - it is not an html page and it is not a regular windows dialog box. It is something else and I cannot configure how to control it with python. It has two buttons and I need to click on one of them (i.e. Continue). It seems like it is a special kind of window implemented in IE. It is distinguished by its title which looks like this: Webpage Dialog -- Download blalblabla. If I click Continue mannually it opens a regular windows dialog box (open,save,cancel) which i know how to handle with winGuiAuto library. Tried to use this library for the Webpage Dialog window with no luck. Tried to recognize the buttons with Autoit Info tool -no luck either. In fact, maybe these are not buttons, but actually links, however I cannot see the links and there is no source code visible... What I need is someone to tell me what this Web page Dialog box is and how to control it with Python. That was my question.
You can't, and you don't want to. When you ask a question, try explaining what you are trying to achieve, and not just the task immediately before you. You are likely barking down the wrong path. There is some other way of doing what you are trying to do.
The title 'Webpage Dialog' suggests that is a Javascript-generated input box, hence why you can't access it via winGuiAuto. What you're asking directly is unlikely to be possible.
However, making the assumption that what you want to do is just download this data from the site, why are you using the GUI at all? Python provides everything you need to download files from the internet without controlling IE. The process you will want to follow is:
Download the host page
Find the url for your download in the page (if it changes)
Download the file from that url to a local file
In Python this would look something like this:
import urllib,re
f = urllib.urlopen('http://yoursitehere') # Original page where the download button is
html = f.read()
f.close()
m = re.search('/[\'"](.*\.xls)["\']/', html, re.S) # Find file ending .xls in page
if m:
urllib.urlretrieve(m.group(1), 'local_filename.xls') # Retrieve the Excel file
It is better to use selenium Python bindings:
from selenium import webdriver
from selenium.webdriver.common import alert
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
class AlertsManager:
def alertsManager(self,url):
self.url_to_visit=url
self.driver=webdriver.Ie()
self.driver.get(self.url_to_visit)
try:
while WebDriverWait(self.driver,1).until(EC.alert_is_present()):
self.alert=self.driver.switch_to_alert()
self.driver.switch_to_alert().accept()
except TimeoutException:
pass
if __name__=='__main__':
AM=AlertsManager()
url="http://htmlite.com/JS006.php" # This website has 2 popups
AM.alertsManager(url)