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

Hey there, I'm trying out a script in perl language using Selenium Chrome Driver as engine. It's a fairly simple code but I got to a part that kept blocking my process for 2-3 days now and I can't seem to get around it. The script consists into logging into an online game account and opening the main page (it's pretty simple as I said, after all it's just exercises for me before I get the hand of it).
use feature ':5.10'; use strict; use warnings; use Selenium::Chrome; use Selenium::Remote::WebElement; my $driver = Selenium::Chrome->new; my $a = 'email'; #write email here my $b = 'password'; #write password here my $login_url = "link/login/"; #link to the website (link is a substi +tute to the actual URL which was considered spam so i just wrote "lin +k") $driver->get( $login_url ); #navigate to the website my $name = $driver->find_element("//input[@name='mailAddress']"); $name->send_keys( $a ); #find and input mail address my $password = $driver->find_element("//input[@name='password']"); $password->send_keys( $b ); #find and input password my $Login_button = $driver->find_element("//input[@value='Login']"); $Login_button->click(); #find and click login button

So far the code is perfect. Now, after reaching this part the struggles started. After logging in an image appears with a ''My Page'' button. This button leads to the my page menu (duh) where I want my script to end at. The source code for the button is below:

<a id="enterBtn" class="push-motion0 js-onceLink" href="link/mypage?t=1pSX7e.3KXy.asX.2k42h.1klg0.280f08.H" style="user-select: none; touch-action: pan-y; -webkit-user-drag: none; -webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></a>

I've tried several things to get past this but I just can't get to navigate to My page and/or ''click'' the button. The thing is the http URL has a base part

"link/mypage?t="

and a token part

"1pSX7e.3KXy.asX.2k42h.1klg0.280f08.H".

The token part changes EVERY time you reopen the page so I CANNOT (or I don't know how to) assign the url to a variable like I did above since it gives me an error upon entering the expired link.

Instead I tried to SEARCH for elements containing the partial link text:

my $my_page = $driver->find_element("//a[@href='link/mypage?t=]"); my $href = $my_page->get_attribute("href"); $driver->get($href); <>;

Also tried 'click' instead of 'get' with same result.

The script stops at the current page without navigating to "My page" menu. The error bellow appears in the .bat that I use to execute the command.

"Use of uninitialized value in substitution iterator at C:/Perl64/site/lib/Selenium/Remote/Commands.pm line 437."

I just can't seem to get past this element and finally open "my page" menu. It has something to do with XPath but I think I'm missing something (simple yet essential).

Replies are listed 'Best First'.
Re: Issues Fetching URL with a variable token
by 1nickt (Canon) on Oct 17, 2017 at 16:00 UTC

    Hi, looks like an XPath question more than Perl.

    Maybe you can find the element by class name?

    //a[contains(concat(' ', normalize-space(@class), ' '), ' push-motion0 + ') and contains(concat(' ', normalize-space(@class), ' '), ' js-onceLink +')]
    ^ untested, ymmv :-)


    The way forward always starts with a minimal test.

      Nope, going with class didn't seem to work, it gave me the same issue as before I tried with a//(contains(@ID = 'enterBtn')) too just in case but I hit the same Dead end.

Re: Issues Fetching URL with a variable token
by wjw (Priest) on Oct 17, 2017 at 20:07 UTC
Re: Issues Fetching URL with a variable token
by holli (Abbot) on Oct 17, 2017 at 20:01 UTC
    Did you try using a regex to grab that token? It looks distinct enough.


    holli

    You can lead your users to water, but alas, you cannot drown them.
      No i just copied the token from the link at that time (it's obviously different each time i open it because it changes every time).
        No, I mean in your code. The token is different each time, but does it follow a pattern? You could figure out a regex that matches the token and let it loose on the page source, parsing out the token that way.


        holli

        You can lead your users to water, but alas, you cannot drown them.
Re: Issues Fetching URL with a variable token
by Petroza (Novice) on Oct 17, 2017 at 22:03 UTC

    Thanks for your advice guys, i read carefully what each of you suggested but apparently the issue was elsewhere...

    I tried printing out the Source code of the page where my script stops and...the source code of the frame containing the ''My Page'' button was not even included in it, meaning I was looking in the wrong place this entire time.

    Looks like I have to swap to another frame (the game frame) and search there for the element.