猿问

Selenium:ElementNotVisibleException

当我测试phptravel网站并尝试使用以下代码单击myaccount链接时。Selenium在执行期间返回ElementNotVisibleException。我错过了什么?


源代码


public void login(WebDriver driver) {

    driver.navigate().to("https://www.phptravels.net/");

    WebDriverWait wait = new WebDriverWait(driver, 10);

    wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/html/body/nav/div/div[1]/a")));

    // Error on here 

    myAccount.click();

    WebDriverWait myAccountWait = new WebDriverWait(driver, 10);

    myAccountWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"li_myaccount\"]/ul")));

    loginLink.click();

    WebDriverWait loginWait = new WebDriverWait(driver, 10);

    //loginWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\\\"loginfrm\\\"]/div[1]/div[5]/div/div[1]/input")));

    username.sendKeys("user@phptravels.com");

    password.sendKeys("demouser");

    loginBtn.click();

}


泛舟湖上清波郎朗
浏览 164回答 3
3回答

largeQ

修改代码如下:在您的 WebDriverWait 上,按类型保持 xpath 如下:By myAccountBy = By.xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a");WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(myAccountBy));或者像下面这样对 xpath 进行硬编码。WebDriverWait wait = new WebDriverWait(driver, 10);wait.until(ExpectedConditions.elementToBeClickable(By.Xpath("//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")));然后为myAccount WebElement保留相同的 xpath,如下所示@FindBy(xpath="//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a")public WebElement myAccount;简而言之,要单击 MyAccount,您必须保留此 xpath//ul[@class='nav navbar-nav navbar-right']/ul/li[1]/a
随时随地看视频慕课网APP

相关分类

Java
我要回答