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!
Related
I would like to point out that I am a beginner and that I started barely 48 hours ago.
I want to code a script that runs a page, enters username and password, click in the login button and clicks in a tab once logged in.
But I encounter an error, it only opens the page for me and does not enter any id/pw and click.
I tried to search and try some changes in the path but it doesn't change anything, and at worst doesn't launch my page anymore, I'm a little lost so I ask for your help please.
enter image description here
From the Selenium docs Finding Web Elements
So in your code,
import By from selenium.webdriver.common.by import By
and use wb.find_element(By.XPATH,"your_xpath")
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.
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 !
I want to learn how to download the CSV files for the last ten years using python. I think this would be helpful.
https://www.usgovernmentspending.com/compare_state_debt
My attempts involve requests and pandas.
This is a multipart problem and I'm going to outline the steps I think you should use.
The first part is going to be simply downloading the webpage. What I would suggest is use something like requests to get the webpage
Once you have that you can use beautiful soup to parse the webpage.
I took a look at the website and it looks like there are a number of ways you could download the data. I think the best way to get the data is going to be to extract all the text from this particular part in the page.
Once you do that you are probably going to need to clean up the data. I suggest using pandas for that.
People on here aren't going to solve the whole problem for you. That said, if you get stuck along the way and have a specific question, StackOverflow can probably help at that point.
Issue resolved I managed to solve it using selenium.
By doing the following:
from selenium import webdriver # allow launching browser
# Opening in incognito
driver_option = webdriver.ChromeOptions()
#driver_option.add_argument(" — incognito")
chromedriver_path = '# Write your path here' # Change this to your own chromedriver path!
# Creating a webdriver.
def create_webdriver():
return webdriver.Chrome(executable_path=chromedriver_path, options=driver_option)
URL = ""
browser.get(url)
# Clicking the button.
elem1 = browser.find_element_by_link_text("download file")
# Clicking the button.
elem1.click()
I put the previous code in a loop for all the years until 2020 and I got all the files in CSV format
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)