简体   繁体   English

“org.openqa.selenium.By”中的“id(java.lang.String)”不能应用于“(org.openqa.selenium.By)”

[英]'id(java.lang.String)' in 'org.openqa.selenium.By' cannot be applied to '(org.openqa.selenium.By)'

I am trying to use By in selenium as a String to get my element but it is giving me the error "'id(java.lang.String)' in 'org.openqa.selenium.By' cannot be applied to '(org.openqa.selenium.By)'"我正在尝试使用 selenium 中的 By 作为字符串来获取我的元素,但它给了我错误“org.openqa.selenium.By”中的“id(java.lang.String)”无法应用于“(org .openqa.selenium.通过)'"

Please assist me Here is my code:请帮助我这是我的代码:

package adta;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class LoginPage extends BasePage {

    public LoginPage(WebDriver driver) {
       super(driver);
    }

    public By getUserNameLocator(){
        return By.id("user-name");

    }


    public void open(){
        driver.get("https://www.saucedemo.com/");

    }
    public boolean isLoaded(){

        return wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(getUserNameLocator()))).isDisplayed();

    }


    public void login(String username, String password) {
        driver.findElement(By.id(getUserNameLocator())).sendKeys(username);
        driver.findElement(By.id("password")).sendKeys(password);
        driver.findElement(By.id("login-button")).click();
    }
}

I have tried to search for answers online but no luck我试图在网上搜索答案,但没有成功

Since your getUserNameLocator() method returns a By type object and ExpectedConditions.visibilityOfElementLocated() method receives a By type value, you can use that inside isLoaded() method as following:由于您的getUserNameLocator()方法返回By类型 object 并且ExpectedConditions.visibilityOfElementLocated()方法接收By类型值,因此您可以在isLoaded()方法中使用它,如下所示:

public boolean isLoaded(){

    return wait.until(ExpectedConditions.visibilityOfElementLocated(getUserNameLocator())).isDisplayed();

}

暂无
暂无

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

相关问题 java.lang.ClassCastException:org.openqa.selenium.By $ ById无法转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: org.openqa.selenium.By$ById cannot be cast to org.openqa.selenium.WebElement java.lang.ClassCastException: 类 org.openqa.selenium.By$ByXPath 不能转换为类 org.openqa.selenium.WebElement - java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement 无法调用“org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)”,因为“this.driver”是 null - Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null 无法调用“org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)”因为“this.searchContext”是 null - Cannot invoke "org.openqa.selenium.SearchContext.findElement(org.openqa.selenium.By)" because "this.searchContext" is null 无法调用“org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)”,因为“this.driver”是 null 请帮帮我 - Cannot invoke "org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null helpme please 为什么我收到错误 java.lang.IllegalArgumentException: Argument is of an invalid type: org.openqa.selenium.By$ByXPath - Why am I getting the error java.lang.IllegalArgumentException: Argument is of an illegal type: org.openqa.selenium.By$ByXPath import org.openqa.selenium.By not found despite of maven 依赖已添加 - import org.openqa.selenium.By not found despite of maven dependency has been added 错误:(34、12)java:不兼容的类型:java.lang.String无法转换为org.openqa.selenium.WebDriver - Error:(34, 12) java: incompatible types: java.lang.String cannot be converted to org.openqa.selenium.WebDriver java.lang.ClassCastException:通过Selenium执行测试时,无法将java.base / java.lang.String强制转换为org.openqa.selenium.WebElement - java.lang.ClassCastException: java.base/java.lang.String cannot be cast to org.openqa.selenium.WebElement when executing test through Selenium WebElement中的sendKeys(java.lang.CharSequence…)无法应用于(org.openqa.selenium.Keys) - sendKeys (java.lang.CharSequence…) in WebElement cannot be applied to (org.openqa.selenium.Keys)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM