java - 无法将 key 发送到页面

标签 java selenium

我正在尝试通过 selenium 登录 https://support.sentinelone.com/。不知怎的,我无法输入我的凭据。这是我的代码。

public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/chromedriver");

        ChromeOptions options = new ChromeOptions();
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://support.sentinelone.com/hc/en-us/restricted?return_to=https%3A%2F%2Fsupport.sentinelone.com%2Fhc%2Fen-us");
        Thread.sleep(5000);
        WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("descendant::*[@class='credentials']/descendant::input[1]")))
                .sendKeys("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fb9682ae889e89bb8e889e89d5989496" rel="noreferrer noopener nofollow">[email protected]</a>");

        driver.findElement(By.cssSelector("#user_password")).sendKeys("myPassword");
        driver.findElement(By.id("sign-in-submit-button")).click();
        Thread.sleep(500);
        List<WebElement> folders = driver.findElements(By.cssSelector(".blocks-item a"));
        System.out.println(folders.size());
        driver.findElement(By.cssSelector(".blocks-item:nth-child(1) a")).getAttribute("href");
    }

我收到的错误为

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: descendant::*[@class='credentials']/descendant::input[1] (tried for 60 second(s) with 500 milliseconds interval) Build info: version: '4.1.0', revision: '87802e897b' System info: host: 'rkeerthi-mba', ip: 'fe80:0:0:0:8ff:bcdc:1025:e4b1%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '11.5', java.version: '16.0.2' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 97.0.4692.99, chrome: {chromedriverVersion: 97.0.4692.71 (adefa7837d02a..., userDataDir: /var/folders/6h/hr_vvn9j0sl...}, goog:chromeOptions: {debuggerAddress: localhost:52226}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), se:cdp: ws://localhost:52226/devtoo..., se:cdpVersion: 97.0.4692.99, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} Session ID: 2f312b7acb65d1e79b013c4f4e05a6db at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:138) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231) at SentinalOneCrawler.main(SentinalOneCrawler.java:24)

不知道我哪里错了。请让我知道如何解决这个问题。

最佳答案

您的定位器是正确的,但 Creds 输入框位于 iframe 中。

因此,首先切换到具有以下 XPath 的 iframe:

//iframe[@frameborder]

然后您就可以访问该元素。

new WebDriverWait(driver, Duration.ofSeconds(30)).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[@frameborder]")));

一旦切换到框架,请使用此行:

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("descendant::*[@class='credentials']/descendant::input[1]")))
            .sendKeys("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f62765a7c6a7d4f7a7c6a7d216c6062" rel="noreferrer noopener nofollow">[email protected]</a>");

此外,使用完 iframe 后,您应该切换回 defaultContent

driver.switchTo().defaultContent();

关于java - 无法将 key 发送到页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70874354/

相关文章:

java - 从 BR 标签中提取文本

java - 无法从 Dropbox 下载文件

java - Commons-Net FTPClient 无法正确下载文件

java - 如何等待 css 属性更改?

Selenium:我可以将多个命令组合成一个命令以供其他测试引用而无需编码

java - 如何使用 selenium WebDriver 将文本插入弹出窗口

java - 在 Eclipse 中拖放可视化编辑器

Java 8 流串行与并行性能

java - Selenium 无法定位 html 页面中的任何元素

python - 如何动态选择python基类?