java 我无法让我的自动化测试用例运行超过“第一”行

guykilcj  于 2023-03-06  发布在  Java
关注(0)|答案(1)|浏览(302)

我正在为我的工作创建一个自动化测试套件,目前我无法让它运行超过第一行,即

"driver.findElement(By.id("username")).sendKeys("example");

我决定尝试谷歌,看看我是否可以取得任何进展,这是我开始的代码

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class fgiLogin {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webDriver.chrome.driver",
"C:\\Users\\examplefolder\\Documents\\examplefolder\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://google.com");

        driver.manage().window().maximize();

        Thread.sleep(5000);

        driver.findElement(By.className("gLFyf")).sendKeys("fruit");
        driver.findElement(By.name("ntnK")).click();

        driver.close();
    }

}

在水果这个词被放进谷歌搜索栏之后,测试就失败了。
这是我在intellij中得到的错误集

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 110.0.5481.77 (65ed616c6e8ee3fe0ad64fe83796c020644d42af-refs/branch-heads/5481@{#839}) on port 10097
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1677870261.213][WARNING]: virtual void DevToolsClientImpl::AddListener(DevToolsEventListener *) subscribing a listener to the already connected DevToolsClient. Connection notification will not arrive.
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='ntnK']"}
  (Session info: chrome=110.0.5481.178)
For documentation on this error, please visit: https://selenium.dev/exceptions/#no_such_element
Build info: version: '4.8.1', revision: '8ebccac989'
System info: os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '19.0.2'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Command: [2937306020c9aa7bccf03e80ec05305a, findElement {using=name, value=ntnK}]
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 110.0.5481.178, chrome: {chromedriverVersion: 110.0.5481.77 (65ed616c6e8e..., userDataDir: C:\Users\ne1723\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:50557}, networkConnectionEnabled: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://localhost:50557/devtoo..., se:cdpVersion: 110.0.5481.178, 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: 2937306020c9aa7bccf03e80ec05305a
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:484)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:200)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:133)
    at org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:53)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:184)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:543)
    at org.openqa.selenium.remote.ElementLocation$ElementFinder$2.findElement(ElementLocation.java:162)
    at org.openqa.selenium.remote.ElementLocation.findElement(ElementLocation.java:66)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:352)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:344)
    at fgiLogin.main(fgiLogin.java:20)

Process finished with exit code 1

我怎样才能让代码越过将键发送到搜索栏的初始行?
试图改变驱动程序。得到的ID,类名,名称,等。我希望谷歌搜索按钮点击和驱动程序关闭预期。搜索栏将进入水果和自动化中断。

iqjalb3h

iqjalb3h1#

一旦您通过 * get() * 调用一个url,然后立即尝试在 * username * 字段中调用 * sendKeys() *,则 * username * 字段可能没有完整的rendered
溶液
理想情况下,要在 * Username * 字段中发送字符序列,需要为elementToBeClickable()引入WebDriverWait,并且可以使用以下locator strategies之一:

  • 使用 * id *:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.id("username"))).sendKeys("jetswept");
  • 使用 * cssSelector *:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("#username"))).sendKeys("jetswept");
  • 使用 * xpath *:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='username']"))).sendKeys("jetswept");

相关问题