Pywinauto search for text on webpage and click on link - python

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.

Related

Recognizing a link on a website to click on it in a using screen visual only

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.

click to javascript button with python

I want write a bot for steam trade. But i cant found example for it with python. I think if i can click a button with python i can do it. Is it true?
If i can click "Accept Trade" button its fine.
I can scrap source with beautifulsoup library but I couldn't any user operation with it.
Here is accept button's source code:
<div id="trade_confirmbtn" class="trade_confirmbtn ellipsis" onclick="ConfirmTradeOffer();">
And here is trade page link hierarchy:
https://steamcommunity.com/tradeoffer/{{tradeid}}/
I have not a code yet. Thank you!
Selenium is a python module that can interact with web-page resources, so that's an easy way to execute JavaScript code from within Python.

Unable to upload file using python selenium webdriver on http://www.dropzonejs.com

I am attempting to upload a file using selenium web driver. I get the file upload dialogue box to open in both MacOS and Windows, after which nothing happens. Wondering why selenium does not open the file via the upload dialog?
Webdriver commands I am using:
wd.get("http://www.dropzonejs.com/")
wd.find_element_by_css_selector("div.dz-message").click()
wd.find_element_by_css_selector("input.dz-hidden-input").click()
elm = wd.find_element_by_xpath("//input[#type='file']")
elm.send_keys("/Users/bg/Downloads/YOURFILE.PDF")
elm.submit()
Don't click the file input element - it would trigger a file upload dialog which you cannot control via selenium. Send the keys to the input and submit the form:
elm = wd.find_element_by_xpath("//input[#type='file']")
elm.send_keys("/Users/bg/Downloads/myfile.PDF")
elm.submit()
submit() in this case is called on an input element - selenium would find the corresponding to the input element form and submit it.
I finally found the code I was looking for to solve my problem. I'm going with 2 hours of research to find a solution to my problem. In my case I needed to send an image of my pc to a program through python. The page only has 1 button to upload the photo and one to send.
Thank you very much for having made the code available
exemple of a program python:
from selenium import webdriver
browser=webdriver.Chrome()
browser.maximize_window()
browser.get(('http://127.0.0.1/namepage.exp'))
elm = browser.find_element_by_xpath('//*[#id="exp_file"]') #
elm.send_keys("C:\PycharmProjects\\varios\image.png")
elm.submit()

how to trigger a button in HTML using python?

I want to trigger a button of a html file.
There is a web site in which there are number of options and a button.
After clicking on the button, using the options, a html table is created on next page.
I want to automate the process but I dont know how I can trigger a button using python.
DO anyone knows about the same?
You can use windmill, mechanize or selenium RC.

How to control Webpage dialog with 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)

Categories

Resources