【问题标题】:How to click the elements insde <md-dailoge-container> angularjs 2 element in selenium如何在 selenium 中单击元素 insde <md-dailoge-container> angularjs 2 元素
【发布时间】:2017-10-14 14:25:46
【问题描述】:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
			<title>Excel To HTML using codebeautify.org</title>
		</head>
		<body>
			<!DOCTYPE html>
			<html>
				<head>
					<meta charset="UTF-8">
						<title>Excel To HTML using codebeautify.org</title>
					</head>
					<body>
						<md-dialog-container class="mat-dialog-container" role="dialog">
							<cdk-focus-trap>
								<!--template bindings={
  "ng-reflect-ng-if": "true"
}-->
								<div tabindex="0"></div>
								<div class="cdk-focus-trap-content">
									<!--template bindings={
  "ng-reflect-portal": null
}-->
									<app-confirmation-dialog _nghost-lok-51="">
										<div _ngcontent-lok-51="" class="box-holder">
											<div _ngcontent-lok-51="" class="header-row text-center-align" fxlayoutalign="space-between center" ng-reflect-klass="header-row" ng-reflect-ng-class="[object Object]" ng-reflect-align="space-between center" style="justify-content: space-between; align-items: center; align-content: center; display: flex; flex-direction: row; box-sizing: border-box; -webkit-box-align: center; -webkit-box-orient: horizontal; -webkit-box-direction: normal;">
												<div _ngcontent-lok-51="" class="header-title "></div>
												<div _ngcontent-lok-51="" class="close-btn">
													<i _ngcontent-lok-51="" class="icon-close"></i>
												</div>
											</div>
											<div _ngcontent-lok-51="" class="box-content">
												<div _ngcontent-lok-51="" class="content-holder text-center-align" fxlayoutalign="start start" ng-reflect-klass="content-holder" ng-reflect-ng-class="[object Object]" ng-reflect-align="start start" ng-reflect-inner-h-t-m-l="Ticket no. 
										
													<a target=&quot;_blank&quot; href=&quot;/v2/tickets/5986813&quot;> 5986813</a> &amp;#160; has been created and is unassigned" style="justify-content: flex-start; align-items: flex-start; align-content: flex-start; display: flex; flex-direction: row; box-sizing: border-box; -webkit-box-pack: start; -webkit-box-align: start; -webkit-box-orient: horizontal; -webkit-box-direction: normal;">Ticket no. 
										
													<a target="_blank" href="/v2/tickets/5986813"> xyz</a> &nbsp; has been created
									
												</div>
												<div _ngcontent-lok-51="" class="action-container" fxlayoutalign="end end" ng-reflect-align="end end" style="justify-content: flex-end; align-items: flex-end; align-content: flex-end; display: flex; flex-direction: row; box-sizing: border-box; -webkit-box-pack: end; -webkit-box-align: end; -webkit-box-orient: horizontal; -webkit-box-direction: normal;">
													<!--template bindings={
  "ng-reflect-ng-if": null
}-->
													<button _ngcontent-lok-51="" class="od-button small action-button" style="margin-right: 0;" type="button"> Ok </button>
												</div>
											</div>
										</div>
									</app-confirmation-dialog>
								</div>
								<!--template bindings={
  "ng-reflect-ng-if": "true"
}-->
								<div tabindex="0"></div>
							</cdk-focus-trap>
						</md-dialog-container>
					</body>
				</html>
			</body>
		</html>

在上面的代码 sn-p 我想执行以下操作 1.我要读取票证ID 2.点击是按钮

我尝试了多种解决方案,但收到错误消息 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素: 我用 xpath 作为 1. //a[包含(@href,"/v2/tickets/")] 2. //div[normalize-space(text())='票号']/a

请有人帮我找到解决方案

【问题讨论】:

    标签: angularjs selenium selenium-webdriver


    【解决方案1】:

    尝试使用下面的代码和 Explicit wait 等待元素出现:

        By ticketID = By.xpath("//a[contains(@href,'/v2/tickets/')] ");
        By okButton = By.cssSelector("button.od-button.small.action-button");
    
        WebDriverWait wait = new WebDriverWait(driver, 30);
    
        wait.until(ExpectedConditions.presenceOfElementLocated(ticketID));
    
        driver.findElement(By.xpath("//a[contains(@href,'/v2/tickets/')] ")).getText();
    
        wait.until(ExpectedConditions.presenceOfElementLocated(okButton));
    
        driver.findElement(By.cssSelector("button.od-button.small.action-button")).click();
    

    【讨论】:

    • 谢谢@kushal。它奏效了。但我没有使用 presentOfElementLocated,而是使用了 waituntileelementvisible/isvisble。
    • Hmm.. 这里的重点是在使用 Angular JS 元素时,您通常必须等到 Angular JS 对象加载完毕,这可能是几种预期条件中的任何一种形式。
    猜你喜欢
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 2021-03-12
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多