0

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)]
Auto-learner
  • 1,411
  • 7
  • 26
  • 43
  • BTW that (fixed now) locator looks a bit too broad - it looks like you are trying to assert a device with a certain name and mac address is present, but it will hold true even if the device name is there, and another device has the mac. If more precision is needed, paste the html and we could get a closer match. – Todor Minakov Jun 17 '17 at 05:08

2 Answers2

0

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']
Tomas Chudjak
  • 562
  • 3
  • 11
  • Thanks for your answer but I am getting error while executing the following code . I have tried at first with one xpath to see if its working fine or not . ${xpath_combined}= Set Variable //div[text()='00:04:56:AC:41:F6'] ${count}= Get Matching Xpath Count ${xpath_combined} Run keyword if ${xpath_combined} == ${count} Click Element xpath=//i[@class='fa fa-lg fa-file-text-o grow'] error is Evaluating expression '//div[text()='00:04:56:AC:41:F6'] == 1' failed: SyntaxError: invalid syntax (, line 1) – Auto-learner May 16 '16 at 11:16
  • ah sorry, my fault, forgot to change the evaluation.. it should be Run keyword if ${count} == 1... variable ${xpath_combined} holds the xpath selector, but ${count} is returned value from Get matching xpath keyword. I will update the answer – Tomas Chudjak May 16 '16 at 11:28
0

Though the other solution is fully viable one, here's an alternative which is an it more universal.

It combines two keywords - Get Webelement which returns a selenium object if it exists, and Run Keyword And Return Status - which returns True if a keyword succeeds, False otherwise:

${locator}=    Set Variable   //body[//div[text()='00:04:56:AC:41:F6'] AND //div[contains(text(),'Device-77')]]
${target}=     Set Variable   //i[@class='fa fa-lg fa-file-text-o grow']
${the element is present}=    Run Keyword And Return Status    Get Webelement  ${locator}

Run Keyword If    ${the element is present}    Click Element    ${target}

Another benefit is that you are not constrained by an xpath locator - it'll work with css, id or whatever other strategy.

Alternatively, one could use Element Should Be Visible instead of Get Webelement, but that works only for elements actuaĺly visible, not just present in the DOM.

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60