Javascript 如何查找动态xpath(每次重新加载页面时都会更改)
大家好,我需要帮助来解决这个问题,我正在尝试自动登录Nike BR网站(这与其他网站不同),但每次我重新加载页面时,xpath都会不断更改,有人能帮我解决这个问题吗?谢谢你们 该网站是 您需要单击“登录/Inscreva se”,然后加载输入Javascript 如何查找动态xpath(每次重新加载页面时都会更改),javascript,xpath,css-selectors,puppeteer,Javascript,Xpath,Css Selectors,Puppeteer,大家好,我需要帮助来解决这个问题,我正在尝试自动登录Nike BR网站(这与其他网站不同),但每次我重新加载页面时,xpath都会不断更改,有人能帮我解决这个问题吗?谢谢你们 该网站是 您需要单击“登录/Inscreva se”,然后加载输入 const puppeteer = require ('puppeteer'); const login = '//*[@id="anchor-acessar-unite-oauth2"]'; const emailinput = 'c
const puppeteer = require ('puppeteer');
const login = '//*[@id="anchor-acessar-unite-oauth2"]';
const emailinput = 'changing xpath'
const passwordinput = 'changing xpath'
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080});
await page.goto ('https://www.nike.com.br');
const login= await page.waitForXPath('//*[@id="anchor-acessar-unite-oauth2"]');
await login.click();
const emailinput = await page.type('changing xpath', 'myEmail@outlook.com', { delay: 110 });
const passwordinput = await page.type('changing xpath', 'myEmail@outlook.com', { delay: 110 });
})();
只是不要使用动态部件,而是通过在重新加载之间不会改变的方式来选择元素。就我所知,这些应该是有效的 选择电子邮件地址输入框:
const emailinput = await page.type('//input[@name="emailAddress"]', 'myEmail@outlook.com', { delay: 110 });
const passwordinput = await page.type('//input[@name="password"]', 'password', { delay: 110 });
选择密码输入框:
const emailinput = await page.type('//input[@name="emailAddress"]', 'myEmail@outlook.com', { delay: 110 });
const passwordinput = await page.type('//input[@name="password"]', 'password', { delay: 110 });
更新
现在我明白了为什么上面的方法不能马上奏效。原因是您尝试选择的内容不在顶部框架中,而是在iframe中
要使其工作,您需要将脚本修改为
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
await page.setViewport({ width: 1920, height: 1080 });
await page.goto('https://www.nike.com.br');
const login = await page.waitForXPath('//*[@id="anchor-acessar-unite-oauth2"]');
await login.click();
const frameElement = await page.waitForXPath('//iframe[@id="nike-unite-oauth2-iframe"]');
const frame = await frameElement.contentFrame();
const emailInput = await frame.waitForXPath('//input[@name="emailAddress"]');
const passwordInput = await frame.waitForXPath('//input[@name="password"]');
await emailInput.evaluate((elem) => elem.value = 'myEmail@outlook.com');
await passwordInput.evaluate((elem) => elem.value = 'password');
})();
我将类型
更改为评估
以直接分配值。我发现type
不可靠,没有时间找出原因
更新2
要同时单击登录按钮,您可以将其添加到末尾:
const submitInput = await frame.waitForXPath('//div[contains(@class, "loginSubmit")]/input', { visible: true });
await submitInput.click();
真的很感激!!!!我试了几天,你能再帮我一次吗?我尝试使用等待页面。键盘。按('Enter');完成登录,但它不工作,但提前感谢!!!我需要点击“进入”按钮,但我不知道怎么做,如果你能帮助我,我将非常感激@穆里洛更新了这么多答案!!!所以helpful@Murilo不客气。如果答案对你有效,请投票和/或接受