【问题标题】:how to find the dynamic xpath (changes every time when reload the page)如何找到动态xpath(每次重新加载页面时都会更改)
【发布时间】:2021-05-12 12:59:04
【问题描述】:

大家好,我需要帮助来解决这个问题,我正在尝试自动登录 Nike BR 网站(这与其他网站不同),但每次我重新加载页面时,xpath 都会不断变化,任何人都可以帮助我解决这个问题问题?感谢你们 该网站是https://www.nike.com.br/ 您需要点击“Login / Inscreva-se”然后输入将加载

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 });

    

    

})();

【问题讨论】:

    标签: javascript xpath css-selectors puppeteer


    【解决方案1】:

    只是不要使用动态部分,并通过在重新加载之间没有变化的东西来选择元素。据我所知,这些应该可以工作。

    选择邮箱地址输入框:

    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改成evaluate直接赋值。我发现 type 不可靠,没有时间弄清楚原因。

    更新 2

    还可以单击登录按钮,您可以将其添加到末尾:

    const submitInput = await frame.waitForXPath('//div[contains(@class, "loginSubmit")]/input', { visible: true });
    await submitInput.click();
    

    【讨论】:

    • 真的很感激!!!!我大约有几天试图这样做,你能再帮我一次吗?我尝试使用 await page.keyboard.press('Enter');完成登录,但它不起作用,但提前谢谢!!!
    • 我需要点击“ENTRAR”按钮,但我没有看到这样做的方法,如果您能帮助我,我将非常感激!
    • @Murilo 不客气。如果对您有用,请投票和/或接受答案
    • 好吧!这次我不能投票,因为我没有 15 名声望,但非常感谢你的帮助!!!我只有一个问题可以回答这个问题,但你们开发机器人吗?
    • @Murilo 我明白了,没问题 :) 不,我不开发机器人。
    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-08
    • 2015-08-28
    • 2012-03-09
    相关资源
    最近更新 更多