Selenium Webdriver Java:元素不可见异常
Selenium Webdriver Java : Element Not Visible Exception
我是 Selenium 的新手。
我很难点击以下 link。
我收到一个元素不可见的异常:
org.openqa.selenium.ElementNotVisibleException: element not visible
对于
driver.findElement(By.xpath("//a[@href='/Home/Members']")).click();
我查看了 Whosebug,给出的解决方案对我不起作用。
我的代码如下:
我已经包含了一些健全性检查,我会在 link 点击
时将其删除
public void clickMembersBenefitsFromHasLoggedInScreen() {
if(driver.findElements(By.xpath("//a[@href='/Home/Members']")).size() != 0) {
int okSize=driver.findElements(By.xpath("//a[@href='/Home/Members']")).size();
System.out.println("ok_size "+okSize);
System.out.println("Hello World "+driver.findElement(By.xpath("//a[@href='/Home/Members']")).getTagName());
driver.findElement(By.xpath("//a[@href='/Home/Members']")).click();
}else {
System.out.println("[AerCardHomePageModel][Does Not Have Members Benefits Button]");
}
}
它所作用的HTML页面如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoggedIn - AerCard</title>
<link href="/Content/css?v=XHx4C0AbUkAQdcPtt7lhY9Ne4vCiFZm7FZ_iSLXgqSk1" rel="stylesheet"/>
<script src="/bundles/modernizr?v=wBEWDufH_8Md-Pbioxomt90vm6tJN2Pyy9u9zHtWsPo1"></script>
<script src='https://cdn.polyfill.io/v2/polyfill.min.js'></script>
</head>
<body>
<div id="aerCardApp">
<div class="" style="height:120px; background-color:white">
<div class="pull-left" style="height:75%; width:15%; text-align:center; margin-left: 50px">
<img style="max-height:100%" src="/img/aer lingus.JPG" />
</div>
<div class="pull-right" style="height:75%; width:15%; text-align:center; margin-right: 50px">
<img style="max-height:100%" src="/img/boi.JPG" />
</div>
</div>
<div class="" style="background-color:lightgray">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse pull-right">
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
</div>
</div>
<div class="" style="height:120px; background-color:black">
<div class="container">
<div class="row">
<div class="col-lg-6">
<h3 v-cloak style="color:white; padding-top:15px">Welcome {{Name}} {{AerCardId}}</h3>
<h4 v-cloak style="color:white;">Aer Credit Card Members benefit Section</h4>
</div>
<div v-if="MemberShipDate !== null" class="col-lg-6 pull-right">
<h3 style="color:white; padding-top:15px">Aer Credit Card Membership Date</h3>
<h4 style="color:white;">{{MemberShipDate}}</h4>
</div>
</div>
</div>
</div>
<div v-cloak class="container body-content">
<div class="row">
<div class="col-lg-6">
<h3>Login to your AerClub Account</h3>
<div class="jumbotron" style="margin-top: 35px;">
<div v-show="!isLoggedIn">
<form id="loginForm" v-on:submit.prevent="onSubmit(this)">
<div class="alert alert-danger" v-if="formErrors.length">
<strong>Please correct the following error(s):</strong>
<ul>
<li v-for="error in formErrors">{{ error }}</li>
</ul>
</div>
<div class="form-group">
<label for="formTitle">Username/Email</label>
<input type="text" class="form-control" id="username" v-model:bind="username">
<label for="formFirstName">Password</label>
<input type="password" class="form-control" id="password" v-model:bind="password">
</div>
<button type="submit" class="btn btn-success pull-right">Login</button>
</form>
</div>
<div v-show="isLoggedIn">
<strong>Congratulations</strong>
<p>You have successfully connected your</p>
<p>AerClub rewards account with your Bank</p>
<p>Of Ireland Aer Credit Card</p>
<p><a href="/Home/Members" class="btn btn-primary pull-right">Members Benefits »</a></p>
</div>
</div>
</div>
<div class="col col-lg-6">
<img style="width:100%; padding-left:20px; padding-top:30px" src="/img/cc.png" />
</div>
</div>
<hr />
</div>
<div class="page-footer" style="height:50px">
<div class="row">
<div class="col-xs-12 col-sm-6" style="text-align:center; padding-top:10px">
<span class="txt-color-white"> © 2018 - All Rights Reserved</span>
</div>
</div>
</div>
</div>
<script src="/bundles/jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1"></script>
<script src="/bundles/bootstrap?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1"></script>
<script src="/Scripts/Views/common.js"></script>
<script src="/Scripts/bundle/LoggedIn.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
</body>
</html>
根据 HTML,您已将 link 与文字分享为 会员福利...在您 登录 后可见。因此,要识别 WebElement,您必须按如下方式诱导 WebDriverWait along with ExpectedConditions clause elementToBeClickable:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Members Benefits"))).click();
我不是专家,但我遇到过类似的问题。特别是在 hrefs 中,它有时找不到元素。有时它不会加载整个页面,因此您正在寻找的元素是不可见的。您可以尝试如下显式等待
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement elem = wait7.until(ExpectedConditions.visibilityOfElementLocated(By.id("")));
有时 cssSellector 效果更好。您可以试试 cssSellector。
我是 Selenium 的新手。
我很难点击以下 link。
我收到一个元素不可见的异常:
org.openqa.selenium.ElementNotVisibleException: element not visible
对于
driver.findElement(By.xpath("//a[@href='/Home/Members']")).click();
我查看了 Whosebug,给出的解决方案对我不起作用。
我的代码如下:
我已经包含了一些健全性检查,我会在 link 点击
时将其删除public void clickMembersBenefitsFromHasLoggedInScreen() {
if(driver.findElements(By.xpath("//a[@href='/Home/Members']")).size() != 0) {
int okSize=driver.findElements(By.xpath("//a[@href='/Home/Members']")).size();
System.out.println("ok_size "+okSize);
System.out.println("Hello World "+driver.findElement(By.xpath("//a[@href='/Home/Members']")).getTagName());
driver.findElement(By.xpath("//a[@href='/Home/Members']")).click();
}else {
System.out.println("[AerCardHomePageModel][Does Not Have Members Benefits Button]");
}
}
它所作用的HTML页面如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LoggedIn - AerCard</title>
<link href="/Content/css?v=XHx4C0AbUkAQdcPtt7lhY9Ne4vCiFZm7FZ_iSLXgqSk1" rel="stylesheet"/>
<script src="/bundles/modernizr?v=wBEWDufH_8Md-Pbioxomt90vm6tJN2Pyy9u9zHtWsPo1"></script>
<script src='https://cdn.polyfill.io/v2/polyfill.min.js'></script>
</head>
<body>
<div id="aerCardApp">
<div class="" style="height:120px; background-color:white">
<div class="pull-left" style="height:75%; width:15%; text-align:center; margin-left: 50px">
<img style="max-height:100%" src="/img/aer lingus.JPG" />
</div>
<div class="pull-right" style="height:75%; width:15%; text-align:center; margin-right: 50px">
<img style="max-height:100%" src="/img/boi.JPG" />
</div>
</div>
<div class="" style="background-color:lightgray">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse pull-right">
<ul class="nav navbar-nav navbar-right">
</ul>
</div>
</div>
</div>
<div class="" style="height:120px; background-color:black">
<div class="container">
<div class="row">
<div class="col-lg-6">
<h3 v-cloak style="color:white; padding-top:15px">Welcome {{Name}} {{AerCardId}}</h3>
<h4 v-cloak style="color:white;">Aer Credit Card Members benefit Section</h4>
</div>
<div v-if="MemberShipDate !== null" class="col-lg-6 pull-right">
<h3 style="color:white; padding-top:15px">Aer Credit Card Membership Date</h3>
<h4 style="color:white;">{{MemberShipDate}}</h4>
</div>
</div>
</div>
</div>
<div v-cloak class="container body-content">
<div class="row">
<div class="col-lg-6">
<h3>Login to your AerClub Account</h3>
<div class="jumbotron" style="margin-top: 35px;">
<div v-show="!isLoggedIn">
<form id="loginForm" v-on:submit.prevent="onSubmit(this)">
<div class="alert alert-danger" v-if="formErrors.length">
<strong>Please correct the following error(s):</strong>
<ul>
<li v-for="error in formErrors">{{ error }}</li>
</ul>
</div>
<div class="form-group">
<label for="formTitle">Username/Email</label>
<input type="text" class="form-control" id="username" v-model:bind="username">
<label for="formFirstName">Password</label>
<input type="password" class="form-control" id="password" v-model:bind="password">
</div>
<button type="submit" class="btn btn-success pull-right">Login</button>
</form>
</div>
<div v-show="isLoggedIn">
<strong>Congratulations</strong>
<p>You have successfully connected your</p>
<p>AerClub rewards account with your Bank</p>
<p>Of Ireland Aer Credit Card</p>
<p><a href="/Home/Members" class="btn btn-primary pull-right">Members Benefits »</a></p>
</div>
</div>
</div>
<div class="col col-lg-6">
<img style="width:100%; padding-left:20px; padding-top:30px" src="/img/cc.png" />
</div>
</div>
<hr />
</div>
<div class="page-footer" style="height:50px">
<div class="row">
<div class="col-xs-12 col-sm-6" style="text-align:center; padding-top:10px">
<span class="txt-color-white"> © 2018 - All Rights Reserved</span>
</div>
</div>
</div>
</div>
<script src="/bundles/jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1"></script>
<script src="/bundles/bootstrap?v=2Fz3B0iizV2NnnamQFrx-NbYJNTFeBJ2GM05SilbtQU1"></script>
<script src="/Scripts/Views/common.js"></script>
<script src="/Scripts/bundle/LoggedIn.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/es6-promise@4/dist/es6-promise.auto.min.js"></script>
</body>
</html>
根据 HTML,您已将 link 与文字分享为 会员福利...在您 登录 后可见。因此,要识别 WebElement,您必须按如下方式诱导 WebDriverWait along with ExpectedConditions clause elementToBeClickable:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Members Benefits"))).click();
我不是专家,但我遇到过类似的问题。特别是在 hrefs 中,它有时找不到元素。有时它不会加载整个页面,因此您正在寻找的元素是不可见的。您可以尝试如下显式等待
WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement elem = wait7.until(ExpectedConditions.visibilityOfElementLocated(By.id("")));
有时 cssSellector 效果更好。您可以试试 cssSellector。