search for a containment in xpath

37 views
Skip to first unread message

Sushant Mamgain

unread,
Aug 13, 2016, 5:01:46 AM8/13/16
to caius...@googlegroups.com
Hi,

I am using caius project in my automation.
I am in a situation where I have to check for text containment in xpath.
 But that is not working for me, and didn't foudn in tutorial also.

I am going by this way:

$window element by_xpath //*\[contains(text(), 'resources')\]
 
But it is not working. I am looking other ways also
M i wrong in syntax?

Kindly clarify!!!

Thanks,
Sushant

tobia...@gmail.com

unread,
Aug 14, 2016, 4:43:54 AM8/14/16
to caius-users
Hi,

from all I can tell, this works just fine.

test.html:

<html>
  <head>
    <title>This is the title</title>
  </head>
  <body>
    <div>This is div AA</div>
    <div>This is div BB</div>
    <div>This is div BB</div>
    <div>This is div CC</div>
  </body>
</html>

test.tcl:

package require Itcl
package require Testing
package require WebDriver

itcl::class MyTests {
    inherit Testing::TestObject

    method test_find_by_xpath_containing_text {} {
        set caps [namespace which [WebDriver::Capabilities #auto -browser_name firefox]]
        set session [WebDriver::Session #auto http://127.0.0.1:4444/wd/hub $caps]
        set window [$session active_window]

        $session set_logging_enabled true
        $window set_url file://[pwd]/test.html

        set div_list [$window elements by_xpath {//*[contains(text(), 'BB')]}]
        puts [[lindex $div_list 0] text]

        itcl::delete object $session
    }
}

exit [[MyTests #auto] run $::argv]

output:

* EXERCISING TESTS IN "MyTests"

* Test 1/1: MyTests::test_find_by_xpath_containing_text -------------- START

- Log:
    WebDriver[28eb67df-c5fe-4986-8219-1ed54d3e3e34]: open 'file:///home/tobias/TMP/test.html'
    WebDriver[28eb67df-c5fe-4986-8219-1ed54d3e3e34]: get elements by xpath '//*[contains(text(), 'BB')]'
    WebDriver[28eb67df-c5fe-4986-8219-1ed54d3e3e34]: got elements 0, 1
    WebDriver[28eb67df-c5fe-4986-8219-1ed54d3e3e34]: element 0 text: This is div BB
    This is div BB

- Verdict: PASS                                         00 min 02 sec 013 ms 

Best regards,
Tobias

Reply all
Reply to author
Forward
0 new messages