0

I am using ruby with selenium and my code is as follows:

$num=1
def isElementPresent(xpathExpression)
  [email protected]_elements(:xpath,xpathExpression)

  if (allElements.size==0)
    return true
    end
  else
    return false
  end

  while Demo.isElementPresent(str1+$num.to_s+str2)
    [email protected]_element(:xpath,str1+$num.to_s+str2).text
    $num+=1
    puts "text is:#{text}"
    @driver.find_element(:xpath,str1+$num.to_s+str2).click
    puts @driver.title
    @driver.navigate.back
  end
end

I'm getting an undefined method error for while Demo.isElementPresent(str1+$num.to_s+str2).

Any help is appreciated

0

1 Answer 1

0

As per my understanding, Your condition should be:

if (allElements.size>0)
return true

As if size is 0. That means no element is present.

Also, in this case your function definition should end before you are calling it. Try following code:

$num=1
def isElementPresent(xpathExpression)
  [email protected]_elements(:xpath,xpathExpression)
  if (allElements.size>0)
    return true
  end
  else
    return false
  end
end
while isElementPresent(str1+$num.to_s+str2) do
    [email protected]_element(:xpath,str1+$num.to_s+str2).text
    $num+=1
    puts "text is:#{text}"
    @driver.find_element(:xpath,str1+$num.to_s+str2).click
    puts @driver.title
    @driver.navigate.back
end

Here I have made two changes: First, for the condition I explained about and Second, Ending the definition of function before your loop.

Please let me know if it works as intended.

Sign up to request clarification or add additional context in comments.

2 Comments

error is of undefined method.That means isElementPresent is undefined. I need help for this
I don't think you'd need Demo for calling function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.