如何点击包含特定范围的链接

时间:2016-09-03 00:21:01

标签: html testing automation behat mink

请参阅Behat中的text,帮助我们点击text1

text1是一个唯一的属性名称,我必须使用它来点击链接text

<h3>
<span class="label label-default">text1</span>
<a class="btn btn-xs btn-success btn-style" href="link1">
<span class="icon glyphicon glyphicon-plus" aria-hidden="true"></span>
text
</a>

3 个答案:

答案 0 :(得分:1)

在您的方法上(步骤方法)使用MinkContext对象或直接在FeatureContext中使用:

$cssSelector = //the css value you want to click
$this->getSession()->getPage()->find('css'. $cssSelector)->click();

您可以在“元素”标签中的Chrome控制台中找到您的cssselector值,方法是单击鼠标右键 - &gt;复制 - &gt;复制选择器。

答案 1 :(得分:1)

您可以在此处使用“css”选择器点击“文字”。如果class = icon glyphicon glyphicon-plus,请尝试下面的代码:

$ this-&gt; getSession-&gt; getPage() - &gt; find('css','。icon .glyphicon .glyphicon-plus) - &gt; click();

答案 2 :(得分:-1)

也许你可以试试:

public class Chatbot {


    public Chatbot( )
    {
    }

    /*
     * Generates a variety of responses, based on what the user has stated
     */
    public static void respond( String statement )
    {        
        // use the findKeyword method to check for various cases of user statements
        if( statement.length() == 0 )
        {
            System.out.println( "Please say something :)" );
        }

        else if( findKeyword( statement, "hi" ) > 0 ||
            findKeyword( statement, "hello" ) > 0 ||
            findKeyword( statement, "hey" ) > 0 ||
            findKeyword( statement, "hiya" ) > 0 ||
            findKeyword( statement, "heya" ) > 0 )
            {
                System.out.println( "Hello to you too!" );
            }

        else if( findKeyword( statement, "how are you" ) > 0 ||
                 findKeyword( statement, "hows it going" ) > 0 ||
                 findKeyword( statement, "howre you" ) > 0 ||
                 findKeyword( statement, "how ya doing" ) > 0 ||
                 findKeyword( statement, "yo wassup" ) > 0 ||
                 findKeyword( statement, "hey whats up" ) > 0 || 
                 findKeyword( statement, "whats up" ) > 0 )
            {
                System.out.println( "I'm good, how are you?" );
            }
    }



    /*
     * findKeyword method, returns either a 0 or a 1
     * @ 0 -- keyword not found
     * @ 1 -- keyword found
     */
    public static int findKeyword( String statement, String keyword )
    {

                // This is in case the keyword is not in the statement at all
        if( !statement.contains( keyword ) )
        {
            return 0;
        }


        int position = statement.toLowerCase().indexOf( keyword.toLowerCase() );        // position of the keyword in the statement
        statement = " " + statement.toLowerCase().replaceAll( "\'\",.?", "") + " ";                   // the purpose of this statement is to allow for us to search for specific phrases w/ spaces before and after the keyword

        String sub = statement.substring( position, position + keyword.length() + 1 );  // isolates the keyword with 1 character before and after

        String charBeforeKeyword = sub.substring( 0, 1 );                               // the character before the keyword
        String charAfterKeyword = sub.substring( sub.length() - 1, sub.length() );      // the character after the keyword



        /*
         * Now, we check to see if the characters we isolated before are letters; if they are        * 
         * @ If they are letters...then our keyword is part of a bigger word (e.g. if we searched for "success" and it brought us "successful"
         * @ If they are not letters, then we have found our keyword with punctuation and/or spaces before/after it
         */
        if( (charBeforeKeyword.compareTo( "a" ) < 0 || charBeforeKeyword.compareTo( "z" ) > 0 )
                && (charAfterKeyword.compareTo( "a" ) < 0 || charAfterKeyword.compareTo( "z" ) > 0 ))
        {
            return 1;
        }

        return 0;  

    }


}



import java.util.Scanner;

public class Execute 
{
    public static void main( String [] args )
    {   
        // Variables and Objects
        Chatbot bot = new Chatbot();
        Scanner input = new Scanner( System.in );
        String statement = "";

        // Prompt and get the user's first input
        System.out.println( "Type text to start chatting!" );
        statement = input.nextLine();

        // While the user doesn't say goodbye or some other form of it, respond to user and then get their next response
        while( bot.findKeyword( statement, "bye" ) != 1 &&
               bot.findKeyword( statement, "cya" ) != 1 &&
               bot.findKeyword( statement, "goodbye" ) != 1 &&
               bot.findKeyword( statement, "gtg" ) != 1 )
        {
            bot.respond( statement );
            statement = input.nextLine();
        }
        System.out.println( "Goodbye!" );




    }

}

然后循环每个节点,并用钥匙比较标签和值。

好看。