Search code examples
seleniumrobotframeworkselenium2library

Need help regarding Run Keyword if xpath matches and then click element


I am trying to validate xpath expression and if it is true click element .Following is the code that I am trying to do . Xpath_combined variable returns Boolean : true . Please help me in correcting the syntax or valid expression

enter image description here

Variable

${xpath_combined}       //div[text()='00:04:56:AC:41:F6'] AND //div[contains(text(),'Device-77')]

Keyword

Run keyword if  ${xpath_combined} == "true" Click Element   //i[@class='fa fa-lg fa-file-text-o grow']

Error from console output

Evaluating expression '//div\[text()='00:04:56:AC:41:F6'\] AND //div\[contains(text(),'Gambit-77')\] == "true"' failed: SyntaxError: invalid syntax (<string>, line 1)]

Solution

  • You can create different scenarios how to solve this question. But, you can try this solution:

    Xpath = With xpath, find the the parent element which contains both elements. But this depends on your code structure f.e. Try to find body element, with two child elements containing specific text conditions:

    xpath=//body[//div[text()='00:04:56:AC:41:F6'] AND //div[contains(text(),'Device-77')]]
    

    Set variable the right way or use scalar. Note: we will use Get Matching Xpath Keyword, so we don't need to use 'xpath=' prefix

    ${xpath_combined}=    Set Variable    //body[//div[text()='00:04:56:AC:41:F6'] AND //div[contains(text(),'Device-77')]]
    

    Get Matching Xpath Count It will return 1 occurence of xpath. There is only one body element which passes the xpath expression

    ${count}=    Get Matching Xpath Count    ${xpath_combined} 
    

    Run Keyword If If the xpath count is exactly 1 time, the elements are present on the page and you are allowed to click on icon. Note: don't forget to add xpath= prefix to Click Element locator

    Run keyword if    ${count} == 1    Click Element    xpath=//i[@class='fa fa-lg fa-file-text-o grow']