issue with for loops in robot framework - python-3.x

I am new to Robot Framework and I am trying to implement a for loop where I want to loop through and store all the text values of all a tags.
But I am only able to get the text of one single a tag.
This is my code:
*** Settings ***
Library SeleniumLibrary
*** Variables ***
${url} http://www.practiceselenium.com/practice-form.html
*** Test Cases ***
ExtractLinks
alllinks
close browser
*** Keywords ***
allLinks
open browser ${url}
${allLinksCount} get element count xpath://a
log to console ${allLinksCount}
FOR ${i} IN 1 ${allLinksCount}
${linkText} get text xpath:(//a)[${i}]
log to console ${linkText}
END
This is the output:
==============================================================================
getAllLinks
==============================================================================
ExtractLinks 10
More
ExtractLinks | PASS |
------------------------------------------------------------------------------
getAllLinks | PASS |
1 test, 1 passed, 0 failed
==============================================================================
So there are 10 a tags on the page, but only one is shown, "More" in the output to the console.
Is it all possible to gather all a link text in list instead?

FOR ${i} IN 1 ${allLinksCount} is equivalent to the python code for i in (1, 10). In other words, it will loop exactly twice.
If you're wanting to iterate over the range of numbers between 1 and ${allLinksCount} you should use IN RANGE.
Because xpath counts starting with one instead of zero, you'll need to adjust the numbers slightly:
FOR ${i} IN RANGE 1 ${allLinksCount}+1
${linkText} get text xpath:(//a)[${i}]
log to console ${linkText}
END
-or-
FOR ${i} IN RANGE ${allLinksCount}
${linkText} get text xpath:(//a)[${i+1}]
log to console ${linkText}
END

Related

SED style Multi address in Python?

I have an app that parses multiple Cisco show tech files. These files contain the output of multiple router commands in a structured way, let me show you an snippet of a show tech output:
`show clock`
20:20:50.771 UTC Wed Sep 07 2022
Time source is NTP
`show callhome`
callhome disabled
Callhome Information:
<SNIPET>
`show module`
Mod Ports Module-Type Model Status
--- ----- ------------------------------------- --------------------- ---------
1 52 16x10G + 32x10/25G + 4x100G Module N9K-X96136YC-R ok
2 52 16x10G + 32x10/25G + 4x100G Module N9K-X96136YC-R ok
3 52 16x10G + 32x10/25G + 4x100G Module N9K-X96136YC-R ok
4 52 16x10G + 32x10/25G + 4x100G Module N9K-X96136YC-R ok
21 0 Fabric Module N9K-C9504-FM-R ok
22 0 Fabric Module N9K-C9504-FM-R ok
23 0 Fabric Module N9K-C9504-FM-R ok
<SNIPET>
My app currently uses both SED and Python scripts to parse these files. I use SED to parse the show tech file looking for a specific command output, once I find it, I stop SED. This way I don't need to read all the file (these can get to be very big files). This is a snipet of my SED script:
sed -E -n '/`show running-config`|`show running`|`show running config`/{
p
:loop
n
p
/`show/q
b loop
}' $1/$file
As you can see I am using a multi address range in SED. My question specifically is, how can I achieve something similar in python? I have tried multiple combinations of flags: DOTALL and MULTILINE but I can't get the result I'm expecting, for example, I can get a match for the command I'm looking for, but python regex wont stop until the end of the file after the first match.
I am looking for something like this
sed -n '/`show clock`/,/`show/p'
I would like the regex match to stop parsing the file and print the results, immediately after seeing `show again , hope that makes sense and thank you all for reading me and for your help
You can use nested loops.
import re
def process_file(filename):
with open(filename) as f:
for line in f:
if re.search(r'`show running-config`|`show running`|`show running config`', line):
print(line)
for line1 in f:
print(line1)
if re.search(r'`show', line1):
return
The inner for loop will start from the next line after the one processed by the outer loop.
You can also do it with a single loop using a flag variable.
import re
def process_file(filename):
in_show = False
with open(filename) as f:
for line in f:
if re.search(r'`show running-config`|`show running`|`show running config`', line):
in_show = True
if in_show
print(line)
if re.search(r'`show', line1):
return

How to Iterate through all the rows of Excel sheet in Robot Framework

How to iterate of all the rows of the Excel sheet. ?
below is my code
*** Settings ***
Library SeleniumLibrary
Library ExcelLibrary
Library Collections
*** Variables ***
${path_excel} C:\\User\\Test.xlsx
*** Test Cases ***
Test1
Test the ExcelData
*** Keywords ***
Test the ExcelData
${my_data_as_list}= Create List
#open the Excel ${path_excel}
open excel document filename=${path_excel} doc_id=Sheet2
FOR ${i} IN 20
${my_data}= Read Excel Cell row_num=${i} col_num=1
Log to Console ${my_data}
Append To List ${my_data_as_list} ${my_data}
Log to Console ${my_data_as_list}
END
it's printing nothing
PS C:\Users\User\PycharmProjects\RobotFramework\Automation> robot .\ExcelExtract.robot
==============================================================================
ExcelExtract
==============================================================================
Test1 None
[None]
Test1 | PASS |
------------------------------------------------------------------------------
ExcelExtract | PASS |
1 test, 1 passed, 0 failed
==============================================================================
Output: C:\Users\User\PycharmProjects\RobotFramework\Automation\output.xml
Log: C:\Users\User\PycharmProjects\RobotFramework\Automation\log.html
Report: C:\Users\User\PycharmProjects\RobotFramework\Automation\report.html
I tried all the possible ways searched online, and was also unable to get the solution
Please change
FOR ${i} IN 20
to
FOR ${i} IN RANGE 1 20

Fail2ban custom regular expresion

Trying to add custom rule on regular expression in order to block the below log.
Mar 17 18:46:52 s21409974 named[1577]: client #0x7g246c107030 1.1.1.1#8523 (.): query (cache) './ANY/IN' denied
I did tried with online tools like this one (https://www.regextester.com) but on the fail2ban-regex test command does display like it miss it.
Any suggestion about the rule or about how to better troubleshoot?
Thank in advance
Why do you try to write a custom regex? This message is pretty well matching with original fail2ban filter named-refused:
$ msg="Mar 17 18:46:52 s21409974 named[1577]: client #0x7g246c107030 1.1.1.1#8523 (.): query (cache) './ANY/IN' denied"
$ fail2ban-regex "$msg" named-refused
Running tests
=============
Use failregex filter file : named-refused
Use single line : Mar 17 18:46:52 s21409974 named[1577]: client #0x7...
Results
=======
Prefregex: 1 total
| ^(?:\s*\S+ (?:(?:\[\d+\])?:\s+\(?named(?:\(\S+\))?\)?:?|\(?named(?:\(\S+\))?\)?:?(?:\[\d+\])?:)\s+)?(?: error:)?\s*client(?: #\S*)? (?:\[?(?:(?:::f{4,6}:)?(?P<ip4>(?:\d{1,3}\.){3}\d{1,3})|(?P<ip6>(?:[0-9a-fA-F]{1,4}::?|::){1,7}(?:[0-9a-fA-F]{1,4}|(?<=:):)))\]?|(?P<dns>[\w\-.^_]*\w))#\S+(?: \([\S.]+\))?: (?P<content>.+)\s(?:denied|\(NOTAUTH\))\s*$
`-
Failregex: 1 total
|- #) [# of hits] regular expression
| 1) [1] ^(?:view (?:internal|external): )?query(?: \(cache\))?
`-
Ignoreregex: 0 total
Date template hits:
|- [# of hits] date format
| [1] {^LN-BEG}(?:DAY )?MON Day %k:Minute:Second(?:\.Microseconds)?(?: ExYear)?
`-
Lines: 1 lines, 0 ignored, 1 matched, 0 missed
[processed in 0.01 sec]
But if you need it, here you go (regex interpolated from fail2ban's pref- & failregex):
^\s*\S+\s+named\[\d+\]: client(?: #\S*)? <ADDR>#\S+(?: \([\S.]+\))?: (?:view (?:internal|external): )?query(?: \(cache\))? '[^']+' denied
replace <ADDR> with <HOST> if your fail2ban version is smaller than 0.10.

How do I search and replace in vi to eliminate a random number of random characters preceding a known string?

I have text files which look like this:
0 298047498 /directory1/app/20170417/file1.blob 0 f191
e 6569844 /directory1/app/20170417/file2.blob 0 f191
344 /directory1/app/20170417/file3.blob 0
8946 /directory1/app/20170417/file4.blob 0
196496 /directory1/app/20170417/file5.blob 0
9 182340752 /directory1/app/20170417/file6.blob 0 f191
68802 /directory1/app/20170417/file7.blob 0
I want to remove everything prior to the first / and everything after the file extension.
Results should look like this:
/directory1/app/20170417/file1.blob
/directory1/app/20170417/file2.blob
/directory1/app/20170417/file3.blob
Is there a way to do this using vi search and replace?
This type of question may be better placed here: https://vi.stackexchange.com/
But for now:
Yout can e.g. use a simple vim-macro, in which you collect all the key-strokes you need to edit one line and repeat this macro as many times as you need it.
Here are simply the key-strokes for one line:
dt/WD
d = delete..
t = ..till the first "/"
W = [shift]+[w] jumps to the next Word (after the "file-location-string")
D = [shift]+[d] deletes till the end of the current line
If you want to record this as a macro, do the following, with the keystrokes from above, inbetween - like this:
qmdt/WD[home][down]q
qm = start the recording of a macro in buffer "m"
... key-strokes from above
[home][down] = key [home] followed by [arrow down]-key, to move into the next line (for convenince)
q = end up the macro-recording
Now execute that macro with:
#m
And if you added the [down] key, you can do something like:
7#m
with which you fire your macro 7 times, for all your 7 lines.

Issues trying to understand a HTBasic code

IF Y$="" THEN
REPEAT
INPUT "FILE NAME**strong text** ?",Y$
Y$=TRIM$(Y$)
UNTIL LEN(Y$)<8
END IF
DISP "enr "&Ld$&"\";Y$
Nb_rc=VAL(Mesu$(0,5))+1
ON ERROR CALL Ges_err
CREATE BDAT Y$&"t",VAL(Mesu$(0,5))+2,7*34
1 ASSIGN #Voie_0 TO Y$&"t"
2 OUTPUT #Voie_0;Mesu$(*)
3 ASSIGN #Voie_0 TO *
CREATE BDAT Y$&"v",VAL(Mesu$(0,6)),(VAL(Mesu$(0,5))+1)*8
ASSIGN #Voie_0 TO Y$&"v"
OUTPUT #Voie_0;V(*)
ASSIGN #Voie_0 TO *
OFF ERROR
RETURN
Here is some HTBasic code, I'm not able to understand what those lines (1,2,3) do.
I found out what those lines mean.
Line 1 : Assign the Path of a file in a "Global variable" named #Voie_0
Line 2 : Writing the values of the tab Mesu in the file
Line 3 : Clear the global variable

Resources