问题
So there's a website: https://www.investing.com/equities/pre-market
And the website has a block with an ID: called components_block
That block has rows...
how can I check that in the column of 'Chg.%' the element has a + sign in front of the value/digits.
I figured out it's the 2nd TR and then the 6th class....
so how do I do that?
I tried this:
Should Match Regexp bold pidExt-23176-pcp greenFont ^[+]
(however I don't like that solution, because the class name can change)
By the way, the result of the test says: does not match while when I test the regulard expression at https://pythex.org/ it works just fine.
so what does not match? is the class not found, or is the result of the regex not okay?
I found something that comes close(r)?: http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-framework-python-menu/204-selenium-robot-framework-table-verification
Table Cell Should Contain xpath=//div[@id='components_block']/table 1 1
回答1:
Should Match Regexp
works by checking that a string matches a given regexp - you have to first get the text of the element, and then use this keyword:
${the text}= Get Text your_locator
Should Match Regexp ${the text} ^[+]
The regexp approach is quite good; you could also use a keyword from the BuiltIn library, that in this case will verify the same:
Should Start With ${the text} +
回答2:
Here is my solution for you. My solution below will store all the '+' output into a list. The complete steps are:
1.Find the xpath locator for the 6th column (Chg.%). Then, find how many rows of them. 10 rows are found when I check. See code below. Note how I use this keyword: GET ELEMENT COUNT
${xpath}= Set Variable /html[1]/body[1]/div[5]/section[1]/div[6]/table[1]/tbody[1]/tr//td[6]
${rows_found}= Get Element Count xpath=${xpath}
2.Create an empty list by using keyword: CREATE LIST
${res_list}= Create List
2.Use FOR-IN RANGE loop to iterate over this 10 rows, and only store the result into the list if it has '+' text/character. When I use GET TEXT
keyword, I pass the tr[${row}] in xpath during each iteration. Note how I use these keywords: RUN KEYWORD AND RETURN STATUS
, SHOULD START WITH
, APPEND TO LIST
.
: FOR ${row} IN RANGE 1 ${rows_found}+1
${row_text}= Get Text xpath=/html[1]/body[1]/div[5]/section[1]/div[6]/table[1]/tbody[1]/tr[${row}]/td[6]
${text_contains_plus}= Run Keyword and Return Status Should Start With ${row_text} +
Run Keyword If '${text_contains_plus}' == 'True' Append To List ${res_list} ${row_text}
Log To Console ${res_list}
Log ${res_list}
However, if you need to store two(2) variables, the company name that has the '+' in 6th column and it's text value (eg: +20.11), then you need to add several more lines in my script above, but the most elegant way is to use the DICTIONARY keywords in ROBOT Framework. Refer to this link: http://robotframework.org/robotframework/latest/libraries/Collections.html
来源:https://stackoverflow.com/questions/54628716/robotframework-how-to-check-an-dynamic-element-in-the-top-row-of-a-component-bl