【问题标题】:Unable to sendKeys to a page无法将密钥发送到页面
【发布时间】:2022-01-27 06:41:28
【问题描述】:

我正在尝试通过 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("myUser@user.com");

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

我得到的错误是

线程“主”org.openqa.selenium.TimeoutException 中的异常: 预期条件失败:等待定位元素的可见性 by By.xpath: descendant::*[@class='credentials']/descendant::input[1] (尝试 60 秒,间隔 500 毫秒)构建信息: 版本:'4.1.0',修订:'87802e897b' 系统信息:主机: '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' 驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 {acceptInsecureCerts: false, browserName: chrome, browserVersion: 97.0.4692.99,铬:{chromedriverVersion:97.0.4692.71(adefa7837d02a ...,userDataDir:/var/folders/6h/hr_vvn9j0sl ...}, goog:chromeOptions: {debuggerAddress: localhost:52226}, javascriptEnabled:真,networkConnectionEnabled:假, pageLoadStrategy:正常,平台:MAC,平台名称:MAC,代理: 代理(),se:cdp:ws://localhost:52226/devtoo ...,se:cdpVersion: 97.0.4692.99,setWindowRect:true,strictFileInteractability:false,超时:{implicit:0,pageLoad:300000,脚本:30000}, unhandledPromptBehavior:解雇并通知, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true} 会话 ID: 2f312b7acb65d1e79b013c4f4e05a6db 在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:138) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231) 在 SentinalOneCrawler.main(SentinalOneCrawler.java:24)

不知道我哪里出错了。请告诉我如何解决此问题。

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    您的定位器是正确的,但 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("myUser@user.com");
    

    另外,一旦你完成了 iframe,你应该切换回defaultContent

    driver.switchTo().defaultContent();
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 1970-01-01
      • 2021-04-03
      • 1970-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多