简体   繁体   English

无法使用 Puppeteer 单击 Stripe 结帐按钮

[英]Unable to click the Stripe checkout button with Puppeteer

I am testing my Stripe checkout process with Puppeteer.我正在使用 Puppeteer 测试我的 Stripe 结帐流程。 I managed to fill out the form, however I cannot get Puppeteer to click the "Start trial" button, this one:我设法填写了表格,但是我无法让 Puppeteer 单击“开始试用”按钮,这个:

开始试用按钮

the html of the button is as follows:按钮的html如下:

<button class="SubmitButton SubmitButton--complete" type="submit" data-testid="hosted-payment-submit-button" style="background-color: rgb(0, 116, 212); color: rgb(255, 255, 255);"><div class="SubmitButton-Shimmer SubmitButton--complete-Shimmer" style="background: linear-gradient(to right, rgba(0, 116, 212, 0) 0%, rgb(58, 139, 238) 50%, rgba(0, 116, 212, 0) 100%);"></div><div class="SubmitButton-TextContainer"><span class="SubmitButton-Text SubmitButton-Text--current Text Text-color--default Text-fontWeight--500 Text--truncate" aria-hidden="false">Start trial</span><span class="SubmitButton-Text SubmitButton-Text--pre Text Text-color--default Text-fontWeight--500 Text--truncate" aria-hidden="true">Processing...</span></div><div class="SubmitButton-IconContainer"><div class="SubmitButton-Icon SubmitButton-Icon--current"><div class="Icon Icon--md Icon--square"><svg viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg" focusable="false"><path d="M3 7V5a5 5 0 1 1 10 0v2h.5a1 1 0 0 1 1 1v6a2 2 0 0 1-2 2h-9a2 2 0 0 1-2-2V8a1 1 0 0 1 1-1zm5 2.5a1 1 0 0 0-1 1v2a1 1 0 0 0 2 0v-2a1 1 0 0 0-1-1zM11 7V5a3 3 0 1 0-6 0v2z" fill="#ffffff" fill-rule="evenodd"></path></svg></div></div><div class="SubmitButton-Icon SubmitButton-SpinnerIcon SubmitButton-Icon--pre"><div class="Icon Icon--md Icon--square"><svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" focusable="false"><ellipse cx="12" cy="12" rx="10" ry="10" style="stroke: rgb(255, 255, 255);"></ellipse></svg></div></div></div><div class="SubmitButton-CheckmarkIcon"><div class="Icon Icon--md"><svg xmlns="http://www.w3.org/2000/svg" width="22" height="14" focusable="false"><path d="M 0.5 6 L 8 13.5 L 21.5 0" fill="transparent" stroke-width="2" stroke="#ffffff" stroke-linecap="round" stroke-linejoin="round"></path></svg></div></div></button>

and I am trying to click the button with this:我试图用这个点击按钮:

await page.click(".SubmitButton");

However, it throws an error saying:但是,它会抛出一个错误说:

Node is either not clickable or not an HTMLElement节点要么不可点击,要么不是 HTMLElement

I tried clicking it using selectors of multiple children of the button and had no success.我尝试使用按钮的多个子项的选择器单击它,但没有成功。 I went to the console of the browser and typed in:我去了浏览器的控制台并输入:

var btns = document.getElementsByClassName("SubmitButton");
btns[0].click()

and it just works (the button is clicked and it redirects to the success page), so the selector must be right.它可以正常工作(单击按钮并重定向到成功页面),因此选择器必须是正确的。 I also tried adding different timeouts, but it didn't work either.我也尝试添加不同的超时,但它也不起作用。

Any ideas how to approach this?任何想法如何解决这个问题?

When I hit this issue it's usually because of one of these 3 errors:当我遇到这个问题时,通常是因为以下三个错误之一:

  • element is not present/visible when I click it;当我单击它时,元素不存在/不可见;
  • element is not ready when I click it (maybe some background logic taking a bit a time in the tested website or something);当我点击它时元素还没有准备好(可能一些背景逻辑在测试的网站上需要一些时间或其他东西);
  • it's one of these times where Puppeteer just plays with you;这是 Puppeteer 只是和你一起玩的时候之一;

To get rid of the 2 first reason, make sure you wait for your element to be present, and add some delay before your click (set a high delay for debugging, then you can reduce it as it fits you):要摆脱第 2 个原因,请确保等待元素出现,并在单击之前添加一些延迟(为调试设置高延迟,然后您可以根据需要减少它):

// wait for element
const myElement = await page.waitForSelector(".SubmitButton", {visible: true});
// add some delay
await page.waitForTimeout(5000); // 5s
// click your elem
await myElement.click();

If it still doesn't work, then you hit the 3rd issue, the only workaround I've found for the moment is to click the element using page.evaluate :如果它仍然不起作用,那么您遇到了第三个问题,我目前发现的唯一解决方法是使用page.evaluate单击元素:

const locator = ".SubmitButton";
await page.evaluate( (paramLocator) => {
    document.querySelector(paramLocator).click();
}, locator);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM