简体   繁体   中英

watir-webdriver: how to select (and click) a javascript link?

I'm trying to write a test for a mobile webapp with cucumber+watir-webdriver, but one particular link on a splash message is giving me trouble.

The link I want to select is <a class="btn" href="#">Close button</a> , which is created on the page by javascript.

I tried these selectors:

browser.link :text => 'Close button'
browser.link(:class,"btn")
browser.div(:id,"vf_dialog_desc").link(:class,"btn") # with encompassing div
browser.div(:xpath,"//div[@id='vf_dialog_desc']/descendant::a[text()='Close button']")

However, all of them fail with a variant of the error:

Error: {"message":"Unable to locate an element with the xpath expression (...snip xpath expression...) because of the following error:\nTypeError: Object #<an HTMLDocument> has no method 'evaluate'"}

Strangely enough, browser.html.include? 'Close button' browser.html.include? 'Close button' is evaluating to true, so watir can access the element.

Note that, unlike other similar questions, this page is not in a frame.

The page structure is:

<html>
    <head>...</head>
    <body>
        <div id="home">
        <div id="vf_dialog_holder" class=" show">
            (...)
            <div id="vf_dialog_wrap">
                <h4 id="vf_dialog_head" style="display: block;" class=" vf_dialog_info_icon">Welcome to xpto</h4>
                <div id="vf_dialog_desc">
                    <img src="884857.jpg">
                    <p>Blurb.</p>
                    <a class="btn" href="#">Close button</a>
                </div>
                <div class="clr">
            </div>
         </div>

I am running watir-webdriver (0.6.4) on ruby-2.0.0-p247.

Since the button is created via javascript, I am guessing that watir is trying to interact with the button before it becomes visible. Try explicitly waiting for the element.

If you just want to wait for the link to be present:

browser.link(:class => "btn").wait_until_present

If you want to do something with it (eg click it) once the link appears:

browser.link(:class => "btn").when_present.click

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM