为什么在for循环的第二次迭代中找不到XPATH
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么在for循环的第二次迭代中找不到XPATH相关的知识,希望对你有一定的参考价值。
我目前正在进行自动化检查圆环图并迭代甜甜圈上的每种颜色。有一种功能,如果你将鼠标悬停在另一种颜色上,其他所有颜色将不透明度增加30%。所以我使用嵌套的for循环来检查它。
见下文
我的问题是在for循环的第二次迭代中找不到xpath。但是,当我试图手动搜索它是trhere和xpath是正确的。
任何人都可以在我的代码中看到问题?先感谢您。
这是实际的html:
<div _ngcontent-c23="" class="col-xs-12 col-sm-6 col-md-6 col-lg-4 space-column ng-star-inserted" data-index="0">
<cws-cardbox _ngcontent-c23="" _nghost-c29="" class="Mutual Funds0" ng-reflect-ng-class="Mutual Funds0" ng-reflect-status="B"><div _ngcontent-c29="" class="card-status overdue" ng-reflect-klass="card-status" ng-reflect-ng-class="[object Object]">
</div>
<div _ngcontent-c29="" class="card-box">
<div _ngcontent-c23="" class="policy">
<span _ngcontent-c23="" class="planType" style="display: block;">Mutual Funds</span>
<span _ngcontent-c23="" class="product" style="display: block;">
Affluence Builder Gold
</span>
<div _ngcontent-c23="" class="policy-no">
<label _ngcontent-c23="" class="policy-label">
Account No.:
</label>
<span _ngcontent-c23="" class="policy-value">8000 0000 03</span>
</div>
</div>
<div _ngcontent-c23="" class="divider"></div>
<!--bindings={
"ng-reflect-ng-if": "false"
}-->
<!--bindings={
"ng-reflect-ng-if": "true"
}--><div _ngcontent-c23="" class="policy-content ng-star-inserted">
<div _ngcontent-c23="" class="invest-mutual">
<!--bindings={
"ng-reflect-ng-if": "true"
}--><div _ngcontent-c23="" class="donut-content ng-star-inserted">
<div _ngcontent-c23="" class="donut-main">
<mli-donut-chart _ngcontent-c23="" _nghost-c31="" ng-reflect-id="0" ng-reflect-height="226" ng-reflect-width="226" ng-reflect-donut-ratio="0.73444" ng-reflect-raw-data="[object Object]"><div _ngcontent-c31="" class="col-xs-12 col-sm-6 col-md-6 col-lg-6 col-xl-6">
<div _ngcontent-c31="" class="init-container">
<!--bindings={
"ng-reflect-ng-if": "true"
}--><div _ngcontent-c31="" class="init-data ng-star-inserted" ng-reflect-ng-style="[object Object]" style="top: 86.444px; left: 46.0083px; width: 133.983px;">
<div _ngcontent-c31="" class="init-title">
Total fund value:
<div _ngcontent-c31="" class="init-fund-value">
<div _ngcontent-c31="" class="init-fund-amt">
1M
</div>
<div _ngcontent-c31="" class="init-fund-curr">
HKD
</div>
</div>
</div>
</div>
<!--bindings={
"ng-reflect-ng-if": "false"
}-->
<!--bindings={
"ng-reflect-ng-if": "true"
}--><div _ngcontent-c31="" class="mli-donut-chart ng-star-inserted"><svg class="pie mli-donut-chart-svg" width="226" height="226"><g class="mli-donut-chart-graph-main" transform="translate(113,113)"><g><path d="M6.919254415182545e-15,-113A113,113,0,0,1,33.30733470843219,-107.9797270538339L24.462238903260936,-79.30463073741777A82.99172,82.99172,0,0,0,5.0817772126866686e-15,-82.99172Z" fill="#004427" class="mli-donut-chart-arc mli-donut-chart-arc-0" style="fill-opacity: 1; outline: 0px;"></path></g><g><path d="M33.30733470843219,-107.9797270538339A113,113,0,0,1,110.16685407654606,25.144865537063552L80.91094430797848,18.467395045040956A82.99172,82.99172,0,0,0,24.462238903260936,-79.30463073741777Z" fill="#00A758" class="mli-donut-chart-arc mli-donut-chart-arc-1" style="fill-opacity: 1; outline: 0px;"></path></g><g><path d="M110.16685407654606,25.144865537063552A113,113,0,1,1,-2.0757763245547636e-14,-113L-1.5245331638060004e-14,-82.99172A82.99172,82.99172,0,1,0,80.91094430797848,18.467395045040956Z" fill="#5CD790" class="mli-donut-chart-arc mli-donut-chart-arc-2" style="fill-opacity: 1; outline: 0px;"></path></g></g></svg></div>
</div>
</div></mli-donut-chart>
</div>
<div _ngcontent-c23="" class="donut-fund-date">
Fund info as of 1 Mar 2019
</div>
</div>
<!--bindings={
"ng-reflect-ng-if": "false"
}-->
<!--bindings={
"ng-reflect-ng-if": "false"
}-->
</div>
</div>
</div>
</cws-cardbox>
</div>
这是我的脚本检查它:
public void hoverOverDonutChart(String policyNumber) {
try {
Thread.sleep(8000);
DriverUtils.getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
iGetTotalPolicyCard = DriverUtils.getDriver().findElements(By.xpath("*//div[contains(@class, 'card-box')]")).size();
System.out.println("This is the total number of card index: " + iGetTotalPolicyCard);
if (iGetTotalPolicyCard == 0) {
Reporter.log("The policy cards are not loaded in the page.", MessageTypes.Fail);
System.out.println("The policy cards are not loaded in the page.");
}else {
for(int i = 1; i <= iGetTotalPolicyCard; i++) {
dataIndex = i - 1;
actualPolicyNumber=DriverUtils.getDriver().findElement(By.xpath("*//div[@data-index = '"+ dataIndex +"']/descendant::span[contains(@class, 'policy-value')]")).getText();
if (policyNumber.equals(actualPolicyNumber)) {
Reporter.log("Policy number: "+ policyNumber + " found in Card no.: " + i, MessageTypes.Info);
System.out.println("Policy number: "+ policyNumber + " found in Card no.: " + i);
DriverUtils.getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
totalNumberOfFund = DriverUtils.getDriver().findElements(By.xpath("*//div[@data-index = '" + dataIndex + "']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@class,'mli-donut-chart')]")).size();
for(int x = 1; x <= totalNumberOfFund; x++) {
System.out.println("x: "+ x + " totalNumberOfFund: " + totalNumberOfFund);
WebElement hoverUnfocus = DriverUtils.getDriver().findElement(By.xpath("//*[contains(text(),'Portfolio')]"));
Actions action= new Actions(DriverUtils.getDriver());
action.moveToElement(hoverUnfocus).build().perform();
DriverUtils.getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement fundAllocation = DriverUtils.getDriver().findElement(By.xpath("(*//div[@data-index = '" + dataIndex + "']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 1')])["+ x +"]"));
action.moveToElement(fundAllocation).build().perform();
Thread.sleep(8000);
Reporter.log("allocation # " + x + " is hovered");
int y=0;
for(int b = 1; b <= totalNumberOfFund; b++) {
if(x!=b) {
y=y+1;
DriverUtils.getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(5000);
WebElement hoveredPortion = DriverUtils.getDriver().findElement(By.xpath("(*//div[@data-index = '" + dataIndex + "']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 0.3')])["+ y +"]"));
if(hoveredPortion!=null) {
Reporter.log("Passed: allocation #: " + b, MessageTypes.Pass);
} else {
Reporter.log("Failed", MessageTypes.Fail);
}
}
}
}
break;
}else {
if(iGetTotalPolicyCard.equals(i)) {
Reporter.log("The card with policy number : " + policyNumber + " is not found", MessageTypes.Fail);
}
}
}
}
}catch(Exception e) {
Reporter.log(e.getMessage(),MessageTypes.Fail);
}
}
似乎在最后一个for循环中遇到了这个问题,因为当我将它评论出来时,它没有错误就可以正常工作,它会徘徊在甜甜圈中的所有项目。
在for循环中发生错误,在第二次迭代时,找不到xpath。
(这是脚本的一部分 - 这是循环的最后一个)
int y=0;
for(int b = 1; b <= totalNumberOfFund; b++) {
if(x!=b) {
y=y+1;
DriverUtils.getDriver().manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(5000);
WebElement hoveredPortion = DriverUtils.getDriver().findElement(By.xpath("(*//div[@data-index = '" + dataIndex + "']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 0.3')])["+ y +"]"));
if(hoveredPortion!=null) {
Reporter.log("Passed: allocation #: " + b, MessageTypes.Pass);
} else {
Reporter.log("Failed", MessageTypes.Fail);
}
}
这是结果中的错误:
no such element: Unable to locate element: {"method":"xpath","selector":"(*//div[@data-index = '0']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 0.3')])[1]"}
(Session info: chrome=72.0.3626.119)
(Driver info: chromedriver=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z'
System info: host: 'PNLCAI731934', ip: '10.242.101.63', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_152'
Driver info: com.qmetry.qaf.automation.ui.webdriver.QAFExtendedWebDriver
Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=false, acceptInsecureCerts=false, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.38.552522 (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb), userDataDir=D:Users
osetluAppDataLocalTempscoped_dir17936_13526}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=72.0.3626.119, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}]
Session ID: 7eacbc5e38368433e8c6d0c617dfda8a
*** Element info: {Using=xpath, value=(*//div[@data-index = '0']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 0.3')])[1]}
答案
我试图改变对象属性,它现在有效。
我使用cssValues而不是检查对象是否存在。
以下是最后一个for循环的变化。
感谢大家研究这个问题。
//WebElement hoveredPortion = DriverUtils.getDriver().findElement(By.xpath("(*//div[@data-index = '" + dataIndex + "']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity: 0.3')])["+ y +"]"));
String notHoveredPortion = DriverUtils.getDriver().findElement(By.xpath("*//div[@data-index = '"+ dataIndex +"']/descendant::div//*[contains(@class, 'mli-donut-chart')]//*[contains(@transform,'translate')]//*[contains(@style,'fill-opacity')]")).getCssValue("fill-opacity");
System.out.println("value of fill-opacity: " +notHoveredPortion);
//if(hoveredPortion!=null) {
if(notHoveredPortion.equals("0.3")) {
Reporter.log("Passed: allocation #: " + b, MessageTypes.Pass);
} else {
Reporter.log("Failed", MessageTypes.Fail);
}
以上是关于为什么在for循环的第二次迭代中找不到XPATH的主要内容,如果未能解决你的问题,请参考以下文章
在 Robot Framework 中找不到 IN 关键字。如何在不使用 IN 关键字的情况下使用 for 循环?