I am trying to set up following actions with Selenium-Webdriver and NodeJs:
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();
Based on the comments, I added "Wait" and "Scroll down", and the code can
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".
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.