How to Click Links via Console by Using XPath and JQuery

Hi all, in this article I will share with you a story. This story comprises a problem and also its solution. :) I joined too many social groups on a social media platform without my acceptance and information. When I realized this situation, I tried to unfollow those groups but doing this operation manually was a nightmare. Thus, I wrote a simple script by using XPath selector and JQuery and execute via the Console and the result was awesome. With a single click, I unfollowed all groups. :)

You can find the magical script below. You can also modify and use this in this kind of situations.

var y = 0, message = '';
while( y < 153 ) { 
    console.log( y );
    $x("//a[text()='Unfollow']")[y].click();
    y = y + 1;
    message = 'y is ' + y;
}

Here is the flow:

  • I wrote a while loop by using JQuery to click all the links
  • I locate the links which comprise of “Unfollow” text by using XPath (//a[text()=’Unfollow’]).
  • I need to wrap that XPath locator by $x(“XPATH Locator”)
  • Then, I select specific element with [$index] 
  • Then, I need to click that element by using .click() method.
  • Final statement is $x(“//a[text()=’Unfollow’]”)[y].click();

Here is the result:

The script, clicked all 153 Unfollow links and I Unfollowed the all the groups. :)

[fusion_widget_area name=”avada-custom-sidebar-seleniumwidget” title_size=”” title_color=”” background_color=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””][/fusion_widget_area]

See you in the next article.
Onur

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.