samberman has asked for the wisdom of the Perl Monks concerning the following question:

I am new to using Selenium, but found that I don't need to "install" Selenium to use this module. I am able to retrieve the page, no error retrieving an element, but when I try to retrieve a child element (an option to click), I receive an error, "An element could not be located on the page using the given search parameters: ./option[@value='50'],xpath at …

I could really use some help. Most of the code is below and anyone can go to the url specified to easily see what I am doing.

A snippet from the web page…

<div class="pageView"> <select onchange="changePriceTablePageSize(parseInt($(this).va +l()), 67287, 'yugioh')"> <option value="10" selected="selected">10 per page</option +> <option value="50">50 per page</option><option value="25"> +25 per page</option> </select>
use Selenium::Firefox; my $url = "http://shop.tcgplayer.com/yugioh/abyss-rising/abyss-dweller +"; my $firefox = Selenium::Firefox->new (); $firefox->set_implicit_wait_timeout(10); # 10 ms. $firefox->get($url); sleep 5; # Couldn't find the "wait… methods" my $content = $firefox->get_page_source(); # can print $content; so far, so good my $parent = $firefox->find_element_by_class('pageView'); my $child = $firefox->find_child_element ($parent, "./option[\@value='50']");

Replies are listed 'Best First'.
Re: Selenium::Firefox: problem finding a child_element
by kcott (Archbishop) on Dec 22, 2015 at 05:43 UTC

    G'day samberman,

    Welcome to the Monastery.

    I searched for pageView in the source of http://shop.tcgplayer.com/yugioh/abyss-rising/abyss-dweller. It wasn't found!

    On the web page, I see a "Login" link. Did you log in before navigating to this page? That could change the content.

    In the source HTML, I see a lot of the content is generated with JavaScript. Furthermore, some of the JavaScript code appears to be dynamically generated; for instance, I see 'var SHIPPINGCOUNTRY = 'AU';' in one <script>...</script> element (I'm in Australia) and that's reflected in the web page under "Shipping Country" (bottom left corner).

    Anyway, without being able to see the same markup as you've posted, here's some things to try:

    • You say you "can print $content". Does that contain the same markup as the "snippet from the web page" you posted?
    • Are there more than one element with the attribute class="pageView"? What does $parent hold?
    • Is your XPath expression correct? Did you perhaps want '//option' or '/select/option' instead of the '/option' part? [Here's the "Abbreviated Syntax" section of the full "XPath Specification"]

    — Ken

      Thank you, Ken and everyone who responded. Ken, you had the answer. I tried my $child = $firefox->find_child_element($elem, "./select/option[\@value='50']"); and then a call to mouse_move_to_location and then to click and when I then retrieved the contents again, the 50 was selected. Thank you.

      Note that if you inspect the element rather viewing the source, you'll be able to see what I represented -- which is also found in the $content variable

      I have yet another, similar problem (to select an option which, in this case is part of an html "li" child element. Maybe you can help. The html is:

      <ul class="listOfChoices"> <li style="display:none" class="clear-filter"><a onclick=" +clearFilter('Condition', 67287, 'yugioh')" href="javascript:void(0)"> +Clear</a></li> <li class=""> <a onclick="changeFilter('Condition', 'NearMint', +67287, 'yugioh')" href="javascript:void(0)"> <span class="option"></span> Near Mint </a> </li> <li class=""> <a onclick="changeFilter('Condition', 'LightlyPlay +ed', 67287, 'yugioh')" href="javascript:void(0)"> <span class="option"></span> Lightly Played </a> </li> ….
      and I want to select the "Near Mint" selection. I can find the "listOfChoices" element. I am (as you can tell) not familiar with xpath, but reading through it.

      Also, how does one print an element?

        Glad to see one of my suggestions led to a solution. :-)

        As to your additional questions, I'm unfortunately strapped for time (due to airlines still unwilling to tailor their flight plans to my personal needs). Here's some hurried (and untested) suggestions.

        From the Abbreviated Syntax link I provided above, see

        • /book/chapter[5]/section[2] selects the second section of the fifth chapter of ...
        • It looks like you need li[2] (note these indices are 1-based - as opposed to 0-based array indices).

        • text() selects all text ...

          Possibly answers "how does one print an element?".

        A recent post I made (Re^2: Trouble capturing multiple groupings in regex) has some additional XPath examples. In particular, see the "more involved for loop" (in the spoiler) for printing an entire element.

        — Ken

Re: Selenium::Firefox: problem finding a child_element
by Athanasius (Archbishop) on Dec 22, 2015 at 04:38 UTC

    Hello samberman, and welcome to the Monastery!

    When I open the given url (in Google Chrome) and select “View page source,” a search finds no elements beginning <div class="pageView">. So, a suggestion: Have you tried printing the value of $parent before the call to initialize $child, to verify that the parent “pageView” node has actually been found?

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you for your response.

      If you "inspectElement" rather than view the page source, you'll see what I saw and it is in the $content variable. Ken, above actually had the answer to the query -- please check above, but if you look above, you'll see another small query.

      Also, how does one print the value of the element that I have in the variable, $parent?

      Thanks again -- Your time and information is truly appreciated.