check if an element exists before assigning its value to a variable

60 views
Skip to first unread message

riks...@gmail.com

unread,
Jan 25, 2016, 2:57:17 PM1/25/16
to nokogiri-talk
So i've been trying to find a way to test an element before assigning its value (text in this case) to a variable but cant find anything like it. Tried looking in the documentation and also searching here and couldn't find anything either. Last time i did it without checking it just threw an error and stopped the script. Can somebody help me?

Thank you!

Mike Dalessio

unread,
Jan 25, 2016, 2:58:51 PM1/25/16
to nokogiri-talk
Hi,

Thanks for asking this question!

I'm not totally sure what you're trying to do. Can you show us your code? Then we might both understand the problem better and be able to make concrete suggestions.

Thanks!
-m


On Mon, Jan 25, 2016 at 1:56 PM, <riks...@gmail.com> wrote:
So i've been trying to find a way to test an element before assigning its value (text in this case) to a variable but cant find anything like it. Tried looking in the documentation and also searching here and couldn't find anything either. Last time i did it without checking it just threw an error and stopped the script. Can somebody help me?

Thank you!

--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To post to this group, send email to nokogi...@googlegroups.com.
Visit this group at https://groups.google.com/group/nokogiri-talk.
For more options, visit https://groups.google.com/d/optout.

riks...@gmail.com

unread,
Jan 25, 2016, 4:40:21 PM1/25/16
to nokogiri-talk
mmm lets see if i can explain myself better... im trying to do this:  variable = browser.element(:css => '.class').text  but the problem comes when the element im trying to scrape doesnt exist because im trying to use the same script for diferent pages. Last time i tried just putting the element even if it didnt exist in some of the pages and it resulted in the script stopping. I've been looking for a way to test elements since so i can assign the value of that element only if it exists if not just do whatever. The only information related to that i could find was something like css_at and xpath_at but im not sure how to use them or if they do what i need.

Thank you for anwsering so quick! i expected this to take a while.

Mike Dalessio

unread,
Jan 25, 2016, 6:04:59 PM1/25/16
to nokogiri-talk
Hi,

Just so I understand, are you using Nokogiri, or are you using some sort of browser-emulation layer like Mechanize or Capybara?

Your snippet "browser.element(:css => 'class')" does not match the Nokogiri API, and so I'd like to make sure I understand what you're currently doing, and what you desire.

Can you please provide your complete attempt to code a solution?

riks...@gmail.com

unread,
Jan 26, 2016, 2:01:39 PM1/26/16
to nokogiri-talk
I solved it, there was an error on the syntax. I was using mechanize with nokogiri, forgot to clarify that. Thank you for the help!
Reply all
Reply to author
Forward
0 new messages