Selenium clicks the line but WATIR doesn't click the same link, So there is a bug the way WATIR identifying the Link

50 views
Skip to first unread message

Raja gopalan

unread,
Mar 26, 2017, 1:18:14 PM3/26/17
to Watir General

I have written the following selenium code to click this link

@driver.find_element(:link,'Recharge').click

It successfully clicks the line

I have written the same code in WATIR as shown below

@b.link(:text,'Recharge').click


but it throws element not present error

I checked out how this above code works, this above code forms this xpath at the end 

"//a[normalize-space()='Recharge']"

So I suspected that this xpath might be the reason, So I have written the below selenium code using this xpath, As I expected the same error was
thrown by selenium as well(element is not visible)

@driver.find_element(:xpath,"//a[normalize-space()='Recharge']").click

So what I think is, WATIR should refrain forming the xpath when b.link is called, they can directly convert this code to @driver.find_element(:link,'Recharge') but they
can form the xpath when b.element(:text,'Recharge') is called

any suggestion?

Titus Fortner

unread,
Mar 26, 2017, 6:05:51 PM3/26/17
to watir-general
Is Watir finding a different link? Or is Selenium just allowing the user to click a link that is not displayed?

There are definitely ways that Watir can optimize its locator strategies, but that part of the code is not trivial to dig into. As always, pull requests are welcome.

--
--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.
 
watir-...@googlegroups.com
http://groups.google.com/group/watir-general
watir-general+unsubscribe@googlegroups.com

---
You received this message because you are subscribed to the Google Groups "Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to watir-general+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raja gopalan

unread,
Mar 27, 2017, 12:38:52 AM3/27/17
to Watir General
Link is clearly visible to click

If I write 

@b.link(:text,'Recharge').click

WATIR converts this code into 

@driver.find_element(:xpath,"//a[normalize-space()='Recharge']").click

but this selenium code says that element is not visible, So it's not working. 

If I write the following selenium code

@driver.find_element(:link,'Recharge').click

It works properly. 

Do you understand me now?




On Sunday, March 26, 2017 at 3:05:51 PM UTC-7, Titus Fortner wrote:
Is Watir finding a different link? Or is Selenium just allowing the user to click a link that is not displayed?

There are definitely ways that Watir can optimize its locator strategies, but that part of the code is not trivial to dig into. As always, pull requests are welcome.
On Sun, Mar 26, 2017 at 12:18 PM, Raja gopalan <rajagopa...@gmail.com> wrote:

I have written the following selenium code to click this link

@driver.find_element(:link,'Recharge').click

It successfully clicks the line

I have written the same code in WATIR as shown below

@b.link(:text,'Recharge').click


but it throws element not present error

I checked out how this above code works, this above code forms this xpath at the end 

"//a[normalize-space()='Recharge']"

So I suspected that this xpath might be the reason, So I have written the below selenium code using this xpath, As I expected the same error was
thrown by selenium as well(element is not visible)

@driver.find_element(:xpath,"//a[normalize-space()='Recharge']").click

So what I think is, WATIR should refrain forming the xpath when b.link is called, they can directly convert this code to @driver.find_element(:link,'Recharge') but they
can form the xpath when b.element(:text,'Recharge') is called

any suggestion?

--
--
Before posting, please read http://watir.com/support. In short: search before you ask, be nice.
 
watir-...@googlegroups.com
http://groups.google.com/group/watir-general


---
You received this message because you are subscribed to the Google Groups "Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to watir-genera...@googlegroups.com.

Titus Fortner

unread,
Mar 27, 2017, 11:48:37 AM3/27/17
to Watir General
Those two locators should find the same element, but it appears it is not if one says the element exists but is not visible and the other says it is visible. To figure out why it is finding a different element, I'd need to see the underlying html.

Raja gopalan

unread,
Mar 27, 2017, 2:21:29 PM3/27/17
to Watir General
Here is the code



require 'watir'
class AirTelRecharge
 
def initialize
 caps
= Selenium::WebDriver::Remote::Capabilities.firefox(marionette: false)
 
@b=Watir::Browser.new :firefox, desired_capabilities: caps, profile: "default"
 
@b.goto "https://www.airtel.in/"
 
@driver=@b.driver
 
@driver.manage.timeouts.implicit_wait=10
 
end

 
def execute
 
@b.element(:text, 'Pay & Recharge').click
 
@driver.find_element(:link,'Recharge').click #Here is the problem this code doesn't work for @b.element(:text,'Recharge').click
 
end
end

AirTelRecharge.new.execute

Titus Fortner

unread,
Mar 27, 2017, 5:13:16 PM3/27/17
to watir-general
I'd need to see the html to see why it works for one of the locator strategies, but not the other.



---
You received this message because you are subscribed to the Google Groups "Watir General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to watir-general+unsubscribe@googlegroups.com.

Raja gopalan

unread,
Mar 28, 2017, 12:13:03 AM3/28/17
to Watir General
Here is the Html code

<document>
<html>
<head>
<body>
<main>
<div class="content-wrap">
<!--Header-->
<header>
<nav id="airtelMegaMenu" class="navbar navbar-default dropdown">
<div class="container-fluid text-right top-smal-menu">
<div class="container-fluid large-menu">
<!-- ==== Label 2 Menus for desktop ==== -->
<!-- mega-menu-pay-recharge -->
<section id="mega-menu-pay-recharge" class="mega-menu-container mega-menu-pay-recharge mega-menu-open">
<a class="mega-menu-close close" href="javascript:void(0);">
<article>
<div class="mega-dropdown-menu">
<!-- ==== Label 2 Menu || Sub Menu for Pay and recharges ==== -->
<ul>
<li>
<li class="link-active">
<a module-id="common" data-i18n="pay_bill" href="javascript:void(0);">Pay Bill</a>
<!-- ==== Label 3 Menu || Sub Menu for Pay bill ==== -->
<ul>
</li>
<li class="">
<li class="">
</ul>
</div>
</article>
</section>
<!-- mega-menu-pay-recharge End-->
<!-- mega-menu-get-->
<section id="mega-menu-get" class="mega-menu-container mega-menu-get">
<!-- mega-menu-get -->
<!-- mega-menu-BANK WITH US-->
<section id="mega-menu-bank" class="mega-menu-container mega-menu-bank">
<!-- mega-menu-BANK WITH US-->
<!-- ==== Label 2 Menus for desktop ==== -->
<!-- ==== Label 2 Menus for Mobile ==== -->
<!-- Mobile mega menu-->
<div id="mobile-mega-menu">
<!-- mega-menu-Bank-mobile -->
<section id="mega-menu-pay-recharge" class="mega-menu-container mega-menu-bank-mobile">
<a class="mega-menu-close back visible-sm visible-xs" href="javascript:void(0);">
<article>
<div class="mega-dropdown-menu">
<ul>
<li>
<li class="link-active">
<a module-id="common" data-i18n="savings_account" href="javascript:void(0);">Savings Account</a>
<ul>
</li>
<li>
</ul>
</div>
</article>
</section>
<!-- mega-menu-Bank-mobile-->
<!-- mega-menu-pay-bill-mobile -->
<section class="mega-menu-container mega-menu-pay-bill">
<section class="mega-menu-container mega-menu-recharge">
<!-- mega-menu-pay-bill-mobile End-->
<!-- mega-menu-Add-money -->
<section class="mega-menu-container mega-menu-add-money">
<!-- mega-menu-Add-money End-->
<!-- mega-menu-Get for me-->
<section class="mega-menu-container mega-menu-get-for-me">
<!-- mega-menu-Get for me End-->
<!-- mega-menu-for-home-mobile -->
<section class="mega-menu-container mega-menu-get-for-home">
<!-- mega-menu-for-home-mobile End-->
<!-- mega-menu-exciting-offers-mobile -->
<section class="mega-menu-container mega-menu-exciting-offers">
<!-- mega-menu-exciting-offers-mobile End-->
<!-- mega-menu-Bank with us mobile -->
<section class="mega-menu-container mega-menu-savings-account">
<!-- mega-menu-Bank with us mobile End-->
<!-- mega-menu-money-wallet mobile -->
<section class="mega-menu-container mega-menu-money-wallet">
<!-- mega-menu-money-wallet mobile End-->
</div>
<!--Mobile mega menu-->
<!-- ==== Label 2 Menus for Mobile End==== -->
</nav>
</header>
<div itemtype="http://schema.org/WebSite" itemscope="">
<section class="banner padding-top-0 padding-bottom-0 airtel-app bg-white home-banner overflow-hidden airtelwow animated">
<div class="airtelwow animated">
<div class="flex-container airtelwow">
<div class="flex-container bg-white airtelwow">
<div class="airtelwow">
<div class="flex-container bg-white airtelwow">
<div class="flex-container bg-white airtelwow">
<!--footer-->
<footer>
</div>
</main>
<script async="" src="app/libs/edited/homepage/airtel-jqlite.js?version=1490500432236"/>
<script async="" src="app/libs/edited/homepage/lazyloading.js?version=1490500432236"/>
<script type="text/javascript">var _gaq = _gaq || [];     _gaq.push(['_setAccount', 'UA-35987772-1']);     _gaq.push(['_setDomainName', 'airtel.in']);     _gaq.push(['_setAllowLinker', true]);     _gaq.push(['_setAllowAnchor', true]);     _gaq.push(['_addIgnoredRef', 'airtel.in']);     _gaq.push(['_trackPageview']);     (function() {      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;     //ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';     ga.src = ('app/libs/edited/homepage/dc.js?version=1490500432236');         var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);     })();</script>
<script type="text/javascript"/>
</body>
</html>
</document>

Titus Fortner

unread,
Mar 28, 2017, 12:32:57 AM3/28/17
to Watir General
This html doesn't have any links with text equal to "Recharge"

Raja gopalan

unread,
Mar 28, 2017, 12:24:16 PM3/28/17
to Watir General
hi, sorry, that was not done properly, here is the html file

<section id="mega-menu-pay-recharge" class="mega-menu-container mega-menu-pay-recharge mega-menu-open">
<a class="mega-menu-close close" href="javascript:void(0);">
<article>
<div class="mega-dropdown-menu">
<!-- ==== Label 2 Menu || Sub Menu for Pay and recharges ==== -->
<ul>
<li>
<li>
<li class="link-active">
<a module-id="common" data-i18n="recharge" href="javascript:void(0);">Recharge</a>
<!-- ==== Label 3 Menu || Sub Menu for Recharge ==== -->
<ul>
</li>
<li>
</ul>
</div>
</article>
</section>
<!-- mega-menu-pay-recharge End-->
<!-- mega-menu-get-->

Titus Fortner

unread,
Mar 28, 2017, 12:34:08 PM3/28/17
to Watir General
Apparently you included the link in an earlier post that I missed. It does look like Watir is grabbing a different, hidden element. I'll try to take a longer look at it tonight.

Titus

Raja gopalan

unread,
Mar 28, 2017, 12:41:27 PM3/28/17
to Watir General
This is the link "https://www.airtel.in/" once you entered, please click "PAY & RECHARGE"   then you will have Recharge option. 

Raja gopalan

unread,
Mar 28, 2017, 12:41:35 PM3/28/17
to Watir General
This is the link "https://www.airtel.in/" once you entered, please click "PAY & RECHARGE"   then you will have Recharge option. 

Justin Ko

unread,
Mar 28, 2017, 4:49:25 PM3/28/17
to Watir General
The difference appears to be that `b.link(:text, 'Recharge')` includes hidden links, where as `driver.find_element(:link, 'Recharge')` only includes visible links.

The page includes 8 links with the matching text:

<a class="mega-menu-link" data-mega-menu="recharge" href="javascript:void(0);">Recharge</a>
<a href="javascript:void(0);" data-i18n="recharge" module-id="common">Recharge</a>
<a target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common" onclick="setCookie('headerlink','Bank WithUs> Savings Account > Manage Account > Recharge')">Recharge</a>
<a target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common" onclick="setCookie('headerlink','Bank WithUs> Wallet > Transact > Recharge')">Recharge</a>
<a target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common" onclick="setCookie('headerlink','Bank WithUs> Savings Account > Manage Account > Recharge')">Recharge</a>
<a target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common" onclick="setCookie('headerlink','Bank WithUs> Wallet > Transact > Recharge')">Recharge</a>
<a onclick="setCookie('headerlink','Savings Account > Transact > Recharge')" target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common">Recharge</a>
<a onclick="setCookie('headerlink','Wallet > Transact > Recharge')" target="_self" href="https://www.airtel.in/netbanking/payments-recharges" data-i18n="recharge" module-id="common">Recharge</a>

However, only the 2nd one, which is the one you want, is visible:

b.links(:text, 'Recharge').map(&:visible?)
#=> [false, true, false, false, false, false, false, false]

You get an exception in Watir because it is clicking on the first one, which is hidden. You could simulate what Selenium is doing by adding the visible locator:

b.link(text: 'Recharge', visible: true).click

Justin

Raja gopalan

unread,
Mar 29, 2017, 3:25:21 AM3/29/17
to Watir General
Ah! Okay, 

The difference appears to be that `b.link(:text, 'Recharge')` includes hidden links, where as `driver.find_element(:link, 'Recharge')` only includes visible links.

Then that's the bug then in WATIR, why not WATIR directly convert this line

b.link(:text,'Recharge')

into

driver.find_element(:link,'Recharge')

And when we write 

b.element(:text,'Recharge')

into

driver.find_element(:xpath,"//*[normalize-space()='Rechage']")

That would be more appropriate,right?

Raja gopalan

unread,
Mar 29, 2017, 3:43:00 AM3/29/17
to Watir General
Continuing to my previous reply

Okay, I just found 

b.element(:link,'Recharge').click


does that Job.


On Tuesday, March 28, 2017 at 1:49:25 PM UTC-7, Justin Ko wrote:

Raja gopalan

unread,
Mar 29, 2017, 3:47:52 AM3/29/17
to Watir General
Titus, I just noticed that watir handled that possibility when I write

b.element(:link,'Recharge').click

The following succeeds because selenium finder includes :link.

if WD_FINDERS.include?(how)



how, what = @selector.to_a.first
selector_builder
.check_type(how, what)

if WD_FINDERS.include?(how)
 wd_find_first_by
(how, what)
else
 find_first_by_multiple
end

Titus Fortner

unread,
Mar 29, 2017, 10:13:36 AM3/29/17
to Watir General
Yes, we discussed some of the peculiarities with matching strings and regex a couple years ago: https://github.com/watir/watir/issues/342

Raja gopalan

unread,
Mar 29, 2017, 12:19:51 PM3/29/17
to Watir General
Yes, WATIR manages extremely well! 
Reply all
Reply to author
Forward
0 new messages