1

I am trying to set up following actions with Selenium-Webdriver and NodeJs:

  • Open Firefox
  • Go to Google.com
  • Search a name in Google
  • Wait for the page to load
  • Select and click the next to the second page.

I am stuck on the last step and have searched similar questions on StackOverflow, but none of the answers have allowed me to locate the or tags for "next" or "page 2". I even copied the completed XPath through inspection, but always received "NoSuchElementError".

Thank you for your help.

const { Builder, By, Key, until, Wait } = require('selenium-webdriver');

require('geckodriver');
const faker = require('faker');

const searchGoogle = async () => {
    let name = faker.name.findName();
    let driver = await new Builder().forBrowser('firefox').build();

    try {
        await driver
            .manage()
            .window()
            .maximize();
        await driver.manage().deleteAllCookies();

        await driver.get('http://google.com');
        await driver.findElement(By.name('q')).sendKeys(name, Key.RETURN);
        await driver.wait(until.titleIs(name), 1000);
        await driver
            .findElement(By.xpath("//span[contains(text(), 'Next')]"))
            .click();
    } catch (err) {
        console.error(err);
    } finally {
        driver.quit();
        console.log('complete!');
    }
};

searchGoogle();

Tony
  • 25
  • 6
  • 1
    Try experimenting with Wait, wait until this 'Next' is displayed or clickable. https://stackoverflow.com/questions/16882860/selenium-webdriver-js-explicit-wait. Locator is definitely correct, just tried in python and it clicked on it for me. – Svetlana Levinsohn Mar 15 '20 at 23:47
  • 1
    @tony i think the issue is that the next button is not in the view , write a script to scroll down in the page before executing the click event . – subramanian Mar 16 '20 at 02:58
  • @subbu, thank you so much. I made the script to do just that based on your suggestion. It works for the most part; however, I still need to use "Wait" for the page to load, I think. Thank you so much. – Tony Mar 17 '20 at 04:38

1 Answers1

0

Based on the comments, I added "Wait" and "Scroll down", and the code can

  • Goes to Google,
  • Search a name
  • Wait for Searched Page to load, Wait for Scroll to Bottom, Wait for next span to click()
const { Builder, By, Key, until } = require('selenium-webdriver');

require('geckodriver');
const faker = require('faker');

const searchGoogle = async () => {
    let name = faker.name.findName();
    let driver = await new Builder().forBrowser('firefox').build();

    try {
        await driver
            .manage()
            .window()
            .maximize();
        await driver.manage().deleteAllCookies();

        await driver.get('https://www.google.com/');
        await driver.findElement(By.name('q')).sendKeys(name, Key.RETURN);

        await driver.wait(() => {
            until.titleIs(`${name}  - Google Search`);
            driver.executeScript('window.scrollTo(0, document.body.scrollHeight)');
            driver.findElement(By.xpath("//span[contains(text(), 'Next')]")).click();
        }, 1000);
    } catch (err) {
        console.error(err);
    } finally {
        // driver.quit();
        console.log('complete!');
    }
};

searchGoogle();

However, now instead of just click the next once to page two, the page goes to page three. I can't really figure the reason behind this. Selenium-webdriver documentation is not very clear on how to use ".wait".

Tony
  • 25
  • 6