Tuesday, January 9, 2018

Google searches for a keyword within a limited number of results pages

Hi, if you are first here, please have a look on the basic tutorial before continue !

This tutorial we work on macros v2 (Only work on the 9Hits Viewer v2 and later).
So, we will search for a keyword on google by flowing steps
1. Open google website
2. Enter the keyword
3. Looking for the result on a limited pages

Ok, let's start !



Open the google site, you can see there is only one textbox. So I will need to click to this textbox, then typing my keyword, then press Enter or click to Search button.


I would like to use ClickByXpath to click to this search textbox. Use the attribute type="text" should be better than other attributes.
await ClickByXpath(GenerateXpath("input", "type", "text"));

To be like human, I will use Tying command. Assuming my keyword is "9hits traffic exchange". the command would be
await Typing ("9hits traffic exchange\r\n");

\r\n at the end of the keyword mean press Enter.
Let try the macros to see how it work, ofc we need to delay to wait for google fully loaded.



await Delay(5000);
await ClickByXpath(GenerateXpath("input", "type", "text"));
await Delay(3000); //I want to wait a bit before typing
await Typing ("9hits traffic exchange\r\n");

The result when you test should be like this


Fortunately, the site we need to find are here, at the first page of results. It's easy to click to one of there links. For example, I want to click to the link has text is 9Hits Panel.
await ClickByXpath("a", "text", "9Hits Panel");

Let test it !

await Delay(5000);await ClickByXpath(GenerateXpath("input", "type", "text"));
await Delay(3000); //I want to wait a bit before typing
await Typing ("9hits traffic exchange\r\n");
await Delay(2000);
await WaitForLoading();
await ClickByXpath(GenerateXpath("a", "text", "9Hits Panel"));

The result

Now, what if the result that you expect did not appear on the page 1 of results !. It's time to looking for the other result pages. There are 2 important things
- Check if we found our target on the current page,
- Go to next page if not

With the keyword "9hits traffic exchange", assuming my expect result on page 3. And I would like to click to the result : Madcashcentral Advertising Pages Exchange

We will use the GetAttribute function for checking the result, a loop to loop through pages until we found the expected result.
Let inspect the next button, Oh, I saw it has id is pnnext, so I will use ClickById to click to next button in case the current page didn't contains my target.
The macro would be like this

await Delay(8000);await ClickByXpath(GenerateXpath("input", "type", "text"));
await Delay(3000); //I want to wait a bit before typing
await Typing ("9hits traffic exchange\r\n");
await Delay(1000);

await WaitForLoading();
page=1;
while(page <= 6) //asuming we only find up to page 6 of results
{
    myTargetXpath = GenerateXpath("a", "href", "%traffic.madcashcentral.com%");
    foundLink = await GetAttribute(myTargetXpath, "href");
    if(foundLink)
    {
        await ClickByXpath(myTargetXpath);
        //if found, click to the target and exit the loop by break keyword
        break;
    }
    else
    {
        //if not found, click to the next button to go to next page
        await ClickById("pnnext");
        await Delay(5000); //wait for a bit to loading next page
        await WaitForLoading();

        page = page+1;
    }
}


Let test it

Then put it into work. Remember! The delay values should be longer than the test, because not all the running viewers in 9hits system has good connection like your local computer (while you testing).



That's all :-)

15 comments:

  1. Hi,
    Turkish karakter problem. Help me please

    ReplyDelete
  2. Nice, but am still confused because I don't know how to write macro

    ReplyDelete
    Replies
    1. Hi, please take a look on the basic tutorial
      http://blog.9hits.com/2017/12/9hits-macros-for-beginer.html

      Delete
  3. hi i m having problem. i am able to go to page after searching on google but after clicking on this page i want click on menu link i.e home but its not working. i m writing code like that:-

    await Delay(3000);
    ClickByXpath(GenerateXpath("input", "type", "text"));
    await Delay(3000); //I want to wait a bit before typing
    Typing ("my keyword");
    SendKeyPress(13);
    await Delay(5000);
    page=1;
    while(page <= 6) //asuming we only find up to page 6 of results
    {
    myTargetXpath = GenerateXpath("a", "href", "%mylink%");
    foundLink = await GetAttribute(myTargetXpath, "href");
    if(foundLink)
    {
    ClickByXpath(myTargetXpath);

    await Delay(5000);// after click i will wait
    ClickByXpath(GenerateXpath("a", "href", "mylink.com"));//click on other link.
    /*my home menu link is like that in inspect window
    li id="menu-item-4670">HOMEli*/

    //if found, click to the target and exit the loop by break keyword
    break;
    }
    else
    {
    //if not found, click to the next button to go to next page
    ClickById("pnnext");
    await Delay(5000); //wait for a bit to loading next page
    page = page+1;
    }
    }


    ReplyDelete
    Replies
    1. in inspect i can see a tag href="link" i m trying to click on that link by ClickByXpath(GenerateXpath("a", "href", "mylink.com")) but not working

      Delete
  4. hi,can y help me I want a macro for shorte.st here on 9hits plz

    ReplyDelete
    Replies
    1. Hi, its already shared here
      https://forum.9hits.com/viewtopic.php?f=7&t=23

      Delete
  5. i want when click on page 1, and then about 80s, it`s continue click anchor text in page 1 to page 2, and finish. and script code google search in here, do you help me, click next page onsite. thank
    await Delay(8000);
    ClickByXpath(GenerateXpath("input", "type", "text"));
    await Delay(3000); //I want to wait a bit before typing
    Typing ("in menu");
    SendKeyPress(13);
    await Delay(5000);
    page=1;
    while(page <= 6) //asuming we only find up to page 6 of results
    {
    myTargetXpath = GenerateXpath("a", "href", "https://kprint.vn/%");
    foundLink = await GetAttribute(myTargetXpath, "href");
    if(foundLink)
    {
    ClickByXpath(myTargetXpath);
    //if found, click to the target and exit the loop by break keyword
    break;
    }
    else
    {
    //if not found, click to the next button to go to next page
    ClickById("pnnext");
    await Delay(5000); //wait for a bit to loading next page
    page = page+1;
    }
    }

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. hi admin. i want random keyword in Typing

    how to?

    ReplyDelete
    Replies
    1. Hi, you may use random keyword like this

      .....
      .....
      var keywords = ["keyword1", "keyword2", "keyword_n"];
      var randomKw = keywords[Math.floor(Math.random() * keywords.length)];
      Typing (randomKw );
      .....
      .....

      Delete
  8. Hello all,
    I have a great idea to bring a visit from a Google source without using these codes
    like this : "https://www.google.com/url?sa=t&rct=j&q=buzz+feed&esrc=s&source=web&cd=1&ved=0ahUKEwiFhu3zge_aAhUG8WMKHc7LAU8QFggnMAA&url=http%3A%2F%2Fwww.lifebuzzmedia.tk&ei=597tWsX2C4bijwPOl4f4BA&usg=AOvVaw2CeUvxb5sck70ygNeXQkVC "

    Will be redirected to my site "http://lifebuzzmedia.tk"
    In addition to keyword :)

    ReplyDelete
  9. what if my site is not show in either of google pages? than?

    ReplyDelete