How to control Webpage dialog with python - python

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)

Related

Pywinauto search for text on webpage and click on link

I am looking how to automate searching for text on a page and then clicking on a link with pywinauto.
Example: open page lingscars.com and search for text "mirage" and click on that link.
I can open the page, but looking for how to find text and then click on it.
from pywinauto import application
import time
import sys
app = application.Application().start("C:\Program Files\Internet Explorer\iexplore.exe https://www.lingscars.com/")
time.sleep(10)
You would need more than python for that.
Selenium is the key for this where you want to control your browser.
Basic info is given here --> https://www.quora.com/How-do-I-make-python-click-buttons-on-websites
But you would need to invest sometime on selenium before you can make it click a single button.
Happy Learning.

Open HTML document in a browser, then update in the SAME window instead of new tab/window

In python you can open a web browser like this...
import webbrowser
webbrowser.open("stackoverflow.com")
This method opens a new tab EVERY time the page is called. I want to create a web page with text boxes, graphic (SVG) devices, etc... then pass variables to it. Basically... use the browser as a display screen.
The HTML page would reside in the same folder with the python code... so this works just fine...
import webbrowser
webbrowser.open("sample.html")
The issue is... if I place this in a timer that updates every second... I get tab after tab... but what I want is for it to open the page ONCE, then just pass data to it as if I had used a SUBMIT button...
My code would generate the appropriate text... URL plus data... then pass it as a long URL.
webbrowser.open("sample.html?alpha=50&beta=100")
The page would pull the variables "alpha" and "beta", then shove the data into some graphic device using javascript. I have had great success manipulating SVG this way... http://askjerry.info/SVG for example.
(Feel free to grab my graphics if you like.)
Is it possible to keep updating a SINGLE page/window instead of a new tab every time??
Thanks,
Jerry
Use the selenium module. The .get() method actually just opens the given url in the same tab and leaves the old url. In fact, I think there's even a .refresh().
From this question: Refresh a local web page using Python
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get('URL')
while True:
time.sleep(20)
driver.refresh()
driver.quit()
Where you can replace your url and parameters with 'URL' - but if you want to pass data from python to html/javascript you will be better off learning flask or something similar. Then you can update your page using ajax which will make your graphics look nicer and will be tractable if you need to pass more data than just alpha and beta.

First paint time in Python (may be using Selenium or without it)?

I want to find the time whatever (an object, image, text, link, DB or anything) loads first in a requested website using Python and Selenium.
Checkout performance.timing, it's JavaScript and comes default in your browser. You have a lot of options to display, like:
navigationStart
connectStart
connectEnd
domLoading
domInteractive
domComplete
Just go to your console window in your browser and type performance.timing. Might be of use to you.
If you find something you can use, you can use selenium to execute the JavaScript inside the browser using execute_script:
driver.execute_script(‘return performance.timing.domComplete’)

Interacting with flash with selenium webdriver Python

I am trying to build a tool for automatic scraping of information from a website that uses flash along with Oracle BI tool. The information is presented as we select things and flash is used to render the images.
Since I cannot scrap information from the images themselves (to my current knowledge), I found out that right clicking on the image, open a menu with "switch to table" and clicking again on that, opens a page with the data on a table scrapable from the HTML tree. My problem is that clicking on flash object (image), like:
from selenium.webdriver.support import expected_conditions as EC
elem = WebDriverWait(browser, 20).until(EC.presence_of_element_located((By.XPATH, '(//embed[#type="application/x-shockwave-flash"])[1]')))
menu=ActionChains(browser).move_to_element_with_offset(elem, 61, 61).click().perform()
But then I try to click on the “switch to table” option but I am not being able to do so, since I believe selenium is still clicking on the image below the menu. What I have tried (with different values for offset and using elem and menu elements with no success):
ActionChains(browser).move_to_element_with_offset(elem, 75, 75).click()
There is any way to do this?
The website is: http://www.adrreports.eu/
Steps to get to the image:
Click language -> search for a report -> pick a letter and then a medicine (new tab appears)-> selecting tab named “Number of individual cases for a selected Reaction”(4th on right top) and then try to get the info from the images on the left
Thank you,
well this is awkward.
my idea was pretty much usable, even though is not very elegant.
With the offset:
menu=ActionChains(browser).move_to_element_with_offset(elem, 61, 61).click().perform()
ActionChains(browser).move_to_element_with_offset(elem, 66, 76).click().perform()
I was able to click on the switch table on the flash menu and make the new page appear with the values.
The main problem was to let the page load, and it seems like it needs quite some time.
time.sleep(10)
time sleep made the trick!
This method work, but if anyone has a better idea for extracting information from a flash image or file, I would love to hear!
Thank you.

How to save a webpage by setting preferences in python for webdriver?

I am currently trying to save a webpage as it appears on the website in html format. Approach I am using is prerssing Ctrl + S using autoit. On pressing that save as dialog box appears where I am asked to enter the name of the file to save. This is working fine. However, I want to save the file by pressing Ctrl + S instead of bring the dialog box in front. I read somewhere by using "set_preference" we can do that. CAn someone suggest how to set a preference. Below is the code I am using for Chrome broswer:
driver=Webdriver.Chrome()
driver.get('http://www.yahoo.com/')
autoit.send("{CTRL down}")
autoit.send("{CTRL down}")
autoit.send("{CTRL up}")
autoit.send("C:\\Users\\karanjuneja\\Downloads\\kj\\ABCD.mhtml")
autoit.send("{ENTER}")
Currently I am using the aboved code, however I want that on pressing Ctrl + S it saves the file in the desired location.
Thanks
Karan
Selenium isn't the designed for this, you could either:
Use getHtmlSource and parse the resulting HTML for references to external files, which you can then download and store outside of Selenium.
Use something other than Selenium to download and store an offline version of a website - I'm sure there are plenty of tools that could do this if you do a search. For example WGet can perform a recursive download (http://en.wikipedia.org/wiki/Wget#Recursive_download)
Is there any reason you want to use Selenium? Is this part of your testing strategy or are you just wanting to find a tool that will create an offline copy of a page?

Categories

Resources