微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Selenium chrome 驱动程序 (Java) click() 登录按钮无法以编程方式工作,而是手动工作

如何解决Selenium chrome 驱动程序 (Java) click() 登录按钮无法以编程方式工作,而是手动工作

我只是在做一些 Web UI 的自动化测试,特别是这个页面 https://autorefi.capitalone.com/login/

我正在查找姓氏、邮政编码、ssn 输入框并输入数据(此处的数据无关紧要)。然后我只是使用定位器单击“登录”按钮。问题是,每次我使用 selenium/chromedriver 在我的代码(Java)中运行它时,我都会收到一个错误

Sorry,we weren't able to log you in. If you continue to see this error,make sure you're using one of our supported browsers.

enter image description here

问题是这不是正确的错误消息。您可以自己尝试,只需打开另一个选项卡并输入随机姓氏、邮政编码和 SSN 的最后 4 位数字即可。相反,如果您确实收到了 Capital one 的报价,则会显示完全不同的页面重点是,我发布的第一条错误消息仅来自 selenium,并且不正确。 正确的错误消息是:

Sorry,it looks like you don't have an offer with Capital one.

enter image description here

我尝试在单击按钮之前休眠线程,因为我认为它可能单击得太快了,但它仍然不起作用。我有点困惑为什么手动执行相同的操作集似乎有效,但通过 selenium 以编程方式启动它。任何人都可以在这里提供任何见解吗?我的代码是:

 WebElement element;
        WebDriver driver = null;
        ChromeOptions options = new ChromeOptions();
        options.setPageLoadStrategy(PageLoadStrategy.NONE);
        driver = new ChromeDriver(options);
        WebDriverManager.getInstance(CHROME).setup();

        // Todo: PROD
        driver.get("https://autorefi.capitalone.com/login/");

        WebElement refiCommonLoginForm = new webdriverwait(driver,10).until(ExpectedConditions.presenceOfElementLocated(By.tagName("refi-common-login-form")));
        WebElement shadowRoot1 = expandRootElement(refiCommonLoginForm,driver);

        WebElement refiCommonLastName = shadowRoot1.findElement(By.tagName("refi-common-last-name"));
        WebElement refiCommonLastNameShadowRoot = expandRootElement(refiCommonLastName,driver);

        WebElement refiCommonZip = shadowRoot1.findElement(By.tagName("refi-common-zip"));
        WebElement refiCommonZipShadowRoot = expandRootElement(refiCommonZip,driver);

        WebElement refiCommonLastFouRSSN = shadowRoot1.findElement(By.tagName("refi-common-last-four-ssn"));
        WebElement refiCommonLastFouRSSNShadowRoot = expandRootElement(refiCommonLastFouRSSN,driver);


        refiCommonLastNameShadowRoot.findElement(By.id("loginLastName")).sendKeys("random last name");
        refiCommonZipShadowRoot.findElement(By.id("loginZipCode")).sendKeys("43978");
        refiCommonLastFouRSSNShadowRoot.findElement(By.id("loginLastFouRSSN")).sendKeys("3483");
        Thread.sleep(2000);
        shadowRoot1.findElement(By.tagName("button")).click();

解决方法

绝对不确定是不是这个问题,但仍然可能会有所帮助。
不要点击“登录”按钮,而是尝试提交它,即 shadowRoot1.findElement(By.tagName("button")).submit()
但我想这里的问题是该站点具有某种反机器人防御功能,可以阻止对其进行自动访问。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。