-
Notifications
You must be signed in to change notification settings - Fork 666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RPA Challenge - code design and optimisation to run 20-30X faster #120
Comments
Hi Pierre, thanks for posting and sharing this! Yes, this post should be here and I appreciate it :) Can you paste the code here? Either copy and paste (you can put code blocks here by putting 3 backticks ``` at the top and below of your code block). Or you can drag and drop your notebook file into the text box here to attach your notebook.
Although this tool is designed to simulate a user action, including the usual user speed in using websites, I'll want to take a look at how could the code be optimised to run this at the fastest speed, and perhaps suggest some hacks that could push the limits further, for the fun of it. And yes, this makes good interesting example to new users who found this RPA package. Will figure out a way to add this example into the readme after we see how it can be optimised. |
Hey kensoh, this is the code block.
The most time consuming part is the typing. Like you said it simulates user action. One possibility which came to my mind is to "copy paste" every single entry, thus we dont do typing. But maybe hard to code... not sure... |
Thanks Pierre, I'll have a look! Copy and paste is easy, something like below would work, just need to make sure the element is in focus. I'll be trying that out. Also, the default design is to simulate normal user reaction time, thus the cycle time for TagUI is between 1-2 seconds per action when communicating with the web browser or using visual automation. This cycle time can be hacked to be set to 0 and may be able to speed things up much faster, depending on the automation scenario. r.clipboard('text to type')
r.keyboard('[ctrl]v') |
Reducing the cycle time sounds really interesting. Copy pasting would only be faster (I guess) if we can copy to clipboard without focussing on the excel sheet. Whats in my mind: read pandas --> entry i --> clipboard entry i --> click xpath xy --> paste entry i |
The source files for TagUI has to be hacked. Will see if that is possible to do the hacking of source files as part of the Python code. But before that, some work-in-progress code - import rpa as r
import pandas as pd
df = pd.read_excel("challenge.xlsx")
df['Phone Number'] = df['Phone Number'].astype(str)
def paste(element_identifier, text_to_paste):
r.click(element_identifier)
r.clipboard(text_to_paste)
r.keyboard('[cmd]v')
r.paste = paste
r.init()
r.url("http://rpachallenge.com")
r.click('//*[text()="Start"]')
for i in range(len(df.axes[0])):
r.paste('//*[@ng-reflect-name="labelPhone"]', df["Phone Number"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='First Name']/following-sibling::INPUT", df["First Name"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Last Name']/following-sibling::INPUT", df["Last Name"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Company Name']/following-sibling::INPUT", df["Company Name"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Role in Company']/following-sibling::INPUT", df["Role in Company"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Address']/following-sibling::INPUT", df["Address"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Email']/following-sibling::INPUT", df["Email"][i])
r.type("//LABEL[@_ngcontent-c2=''][text()='Phone Number']/following-sibling::INPUT", df["Phone Number"][i])
r.click('//*[@value="Submit"]')
r.snap("/html/body/app-root/div[2]/app-rpa1/div", "score.png")
r.wait(10)
r.close() |
Ok I tried it and unfortunately it doesnt fill in the fields when I execute it. |
Oh no, it is not tested code, still work in progress. Above should fail, because init() has to be init(True) in order to use the low-level keyboard and mouse automation. I'll update again when I have a good solution. If I can, I'll post a standard solution for this tool, and another solution for optimised timings 😄 |
Ouh yes I also tried to turn it on, otherwise it gave an error like you said. Ok I am really excited to see the code!! :) |
Below example solves the original RPA Challenge at https://rpachallenge.com # to use Pandas to read Excel, pip install pandas -> pip install xlrd -> pip install openpyxl
import rpa as r
import openpyxl
import pandas as pd
r.init(turbo_mode = True)
r.url('https://rpachallenge.com')
# click to download challenge spreadsheet
r.click('Download Excel')
r.wait()
# load and prepare all data to string
df = pd.read_excel('challenge.xlsx', engine='openpyxl')
df['Phone Number'] = df['Phone Number'].astype(str)
# timer starts after running this step
r.click('//*[text()="Start"]')
# loop through and fill in all fields
for i in range(len(df.axes[0])):
r.type('//*[@ng-reflect-name="labelFirstName"]', df['First Name'][i])
r.type('//*[@ng-reflect-name="labelLastName"]', df['Last Name '][i])
r.type('//*[@ng-reflect-name="labelCompanyName"]', df['Company Name'][i])
r.type('//*[@ng-reflect-name="labelRole"]', df['Role in Company'][i])
r.type('//*[@ng-reflect-name="labelAddress"]', df['Address'][i])
r.type('//*[@ng-reflect-name="labelEmail"]', df['Email'][i])
r.type('//*[@ng-reflect-name="labelPhone"]', df['Phone Number'][i])
r.click('//*[@value="Submit"]')
# page as identifier means the webpage
r.snap('page', 'score.png')
r.wait(10)
r.close() Below example solves Automation Anywhere BotGames Week 2 Challenge # you need to click "Accept All Cookies" button on the popup page for the first run
import pandas as pd
import rpa as r
r.init(turbo_mode = True)
r.url('https://developer.automationanywhere.com/challenges/automationanywherelabs-supplychainmanagement.html')
r.click('Download Agent Territory Spreadsheet')
r.wait()
df = pd.read_excel('StateAssignments.xlsx')
po_numbers = []
for n in range (7):
po_numbers.append(r.read('#PONumber' + str(n+1)))
r.dom('window.open("https://developer.automationanywhere.com/challenges/AutomationAnywhereLabs-POTrackingLogin.html")')
r.popup('POTracking')
r.type('inputEmail', '[clear]admin@procurementanywhere.com')
r.type('inputPassword', '[clear]paypacksh!p')
r.click('(//button)[1]')
orders_list = []
for n in range(7):
r.type('//input[@type = "search"]', '[clear]' + po_numbers[n])
state = r.read('(//table//td)[5]')
ship_date = r.read('(//table//td)[7]')
order_total = r.read('(//table//td)[8]')
orders_list.append([state, ship_date, order_total])
r.popup('supplychainmanagement')
for order in range(7):
r.type('#shipDate' + str(order+1), orders_list[order][1])
r.type('#orderTotal' + str(order+1), orders_list[order][2][1:])
agent_name = df.loc[df['State'] == orders_list[order][0]].iloc[0]['Full Name']
r.select('#agent' + str(order+1), agent_name)
r.click('#submitbutton')
r.wait()
r.close() |
It looks not viable to use clipboard to paste, because it requires clicking visually on the textbox. That takes time to click onto the text box. Let's see for above solution, after tuning how long it would take. |
Your Xpath code is better (more efficient), did not know how to do it like that, just learned about the existence of Xpath few days ago. |
Numpy should be faster than Pandas because Pandas uses Numpy. But the overhead should have little difference in this situation, because the execution time the delay is caused mostly by the automated actions on the web browser, not by accessing of Pandas data frame. There is another tuning that can be done, to enter all text at one go. Let me see how that goes. |
12 seconds :D that is amazing... I really apprecciate that you took the time to do it.. impressive. |
Oh I created the TagUI tool, and RPA for Python is built on TagUI tool. So I know exactly where to hack to tune the performance. Sure, you can definitely mention me but that is optional. Also, there isn't an optimal solution for this for fastest time. Because RPA for Python and TagUI are designed to mimic normal human user action, including the normal human reaction time. So the architecture was always designed for reliability and accuracy, never for speed. Thus with a lot of tuning it still cannot be as fast as some other tools with other design goals. |
No words....... |
The changes I made is -
to
The changes 1 & 2 reduce the delay between the tool to browser from 200ms (100+100) to 0ms. The change 3 changes the default typing behaviour from character by character to typing whole string at one go, ie text gets filled immediately in the target webpage field.
The location of the src folder and files depends on your operating system - #61 (comment) For Windows, location of TagUI is %APPDATA%\tagui For macOS and Linux, it is in ~/.tagui folder If you are using visual automation, there is normal mouse cursor movement from current position to the target position. If you wish to eliminate this mouse movement and 'teleport' the cursor immediately to the target UI element position, run the following after you do r.init(True) - r.vision('Settings.MoveMouseDelay = 0') This works by sending custom code to the SikuliX engine that is used to handle visual automation, OCR, low-level keyboard and mouse capabilities of this tool. Default is 0.5 second movement. Below are other ways to increase speed for visual automation (desktop based automation) -
Above will reduce the intentional communication delay between TagUI and SikuliX engine from 1 second to 0 second. Ie shave off around 1s per action. Lastly, instead of doing
|
Thank you kensoh, I will try it and am excited to see the magic happen! :) |
You're welcome! Let me know if you run into issues tuning above hacks. :) |
It worked and it looks just amazing! |
Thanks Pierre! Added to readme - https://github.com/tebelorg/RPA-Python#api-reference |
Hi @kensoh thanks for the super fast help! You are an extremely great help for me! As soon as I focus Chrome after the init, it works. So you are absolutely right with your assumption! Unfortunately I haven't quite understood the picture yet. I have createt an snap as follow (before creation, I focused the browser):
If I use this png (which is completely white in this case), I get the message, that textbox.png cannot be found. It would be awesome if you could help me! |
Hi @FabianSer I see, try using the Windows Snipping tool to do the screenshot capture of the textbox instead. When you capture, capture a larger area than the textbox, so that it is unique to be matchable using computer vision, versus a blank text box which is hard to match on the screen. Also, for Windows, if visual automation is cranky, try setting your display zoom level to recommended % or 100% - https://github.com/tebelorg/RPA-Python#rpa-for-python-snake |
Hi @kensoh ! Thanks for your help. Even with the larger screenshot capture, it can't be found. I actually think that the file cannot be found. But the screenshot is in the same directory as my jupyter notebook... |
No probs! Try providing the full path to the image file. Alternatively, do |
Guys, HI! |
Other than the changes above - #120 (comment), Below are ways to increase speed for visual automation (desktop based automation) -
Above will reduce the intentional communication delay between TagUI and SikuliX engine from 1 second to 0 second. Ie shave off around 1s per action. Also, in above link, Lastly, instead of doing
Also updated above into the original comment at #120 (comment) |
Thanks for the information and advice! I'll sure keep it in mind. |
You're welcome, have fun! Do note that even with above tweaking, there will still be some lag to do computations on computer vision matching or OCR. Lag time could be a few seconds for an old computer. For newer computer this lag time is negligible. |
Hi, |
Hi @jshah3821 can you share more details on the popup? Is it a new tab or just a popup within the same tab? One way could be adding extra step in between your steps to check if the popup has appeared, then handle accordingly. You can do something like below, and use r.timeout() to set a shorter time so you don't wait too long.
|
Hi, @kensoh Actually, I just started with RPA but I have a good experience working with Python. I have a question that when I run my code it takes a lot of time to open chrome (my default browser is edge). Below is my code, rpa code starts from line 5 def login(request): Also, Please share the relevant resources that I can study to get more in depth knowledge about RPA with Python. |
Hi @tyagiabhinav999 can you try running again? The first time Chrome start may be slower to create the Chrome user profile used by the rpa package. If it is still slow, it might be some computer or company network specific settings that launches Chrome slowly if it is launched with websocket backdoor opened. For visual automation mode True, it is slow because time is needed to load the OpenCV computer vision engine and Tesseract OCR engine into memory. If you don't need these features, do init() without the visual automation True. |
The API section has a few examples and tips on using the package - https://github.com/tebelorg/RPA-Python#api-reference |
@kensoh yes, you're right...it is taking time to load the chrome, if I comment out r.close() and send the request again, it's pretty fast because chrome is always loaded. But, If I do chrome_browser=False, it doesn't work with any other browser. Why is it so ? |
I see, probably environment or company-specific policy causing the Chrome to load slowly. If Chrome browser is needed you cannot do As an alternative, try the TagUI RPA engine below, to see if the slowness happens. And raise the issue there, because the rpa package uses a fork of TagUI engine as the backend automation engine. If there is an issue with loading Chrome slowly, it should be fixed upstream. |
Adding on, the rpa package is only designed to work with Chrome. If you use TagUI from above link, you can automate using MS Edge. |
Also copying @ruthtxh fyi on above issue, which possibly could be inherent in TagUI for some users with edge environments. |
Hello, I would like to ask that tagui starts the Google browser installed in the system by default. If I do not have Google Browser installed in the system but have Google browser installed without installation, can he specify the location to start the specific browser |
It's possible but you have to edit some file to point the user profile folder to your own chrome user. The easiest is you try to login while doing a wait 300 so that the automated browser has your credentials. |
Hey,
this is not really an issue, more a proposal. If this shouldn't be here, I am sorry.
My "issue":
I finished the RPA Challenge (www.rpachallenge.com) with Python RPA and I thought it could be interesting for many People (who are relatively new to coding and XPath) because you have to use relative XPath Code.
My score is not the fastest and Maybe someone has interesting ideas how to improve the time.
I added the Code and the snapshot of the time.
I do not know how to upload Jupyter Notebooks.
And Maybe this RPA Challenge could also be included in the example package for newbies to test their understanding of XPath.
Just a few thoughts..
Best regards
Pierre
The text was updated successfully, but these errors were encountered: