I'm using Watir on a Rails project to scrape the following page: https://icecat.biz/fr/search?keyword=3030050010763
I need to check if the <a>
tag with the 'src-routes-search-product-item-raw-style__descriptionTitle--8-vad'
class exists, which it does, but the .exists? method is returning false
.
b = Watir::Browser.new :chrome
b.goto "https://icecat.biz/fr/search?keyword=3030050010763"
p b.a(:class => "src-routes-search-product-item-raw-style__descriptionTitle--8-vad").exists?
I know the element exists because the following code is returning the href value of that same element:
b.a(:class => "src-routes-search-product-item-raw-style__descriptionTitle--8-vad").href
I've used the .exists? method to check if some elements exist on other pages and it works fine. For example, this is returning true
:
b = Watir::Browser.new :chrome
b.goto "https://icecat.biz/fr/p/babyliss/bab5586e/hair+dryers-3030050010763-bab5586e-29245899.html"
p b.a(:data_type => "json").exists?
I can't seem to figure out what I'm doing wrong, any help would be appreciated.
This is due to the product list being loaded asynchronously. As a result, you will see that #exists?
has a different result based on when you run it:
#exists?
will be false
.#exists?
will be true
.You can see this clearly by checking #exists?
multiple times after loading the page:
b = Watir::Browser.new :chrome
b.goto "https://icecat.biz/fr/search?keyword=3030050010763"
5.times do
p b.a(:class => "src-routes-search-product-item-raw-style__descriptionTitle--8-vad").exists?
end
#=> false
#=> false
#=> true
#=> true
#=> true
Note that methods that interact with the element, such as #href
, will automatically wait for the element to exist before taking action.