带有加号的按钮的唯一名称 - 要单击此按钮

代码是:


<button class="btn btn-default btn-xs" style="padding:0 5px" id="addPiidRelatedNumberBtn_332741000" ng-click="AddPiidRelatedNumber($event)" ng-show="EnableEditable &amp;&amp; CanEdit">

<span class="glyphicon glyphicon-plus" title="Add New Related Number"></span>

我在 STS 包中编写了这个 selenium 代码:


WaitUtility.ThreadSleep(1000);

driver.findElement(By.xpath("//button[@id='addPiidRelatedNumberBtn_332741000']")).click();

但我知道 id 可以随时更改,因为它有一些数字。有没有更好的方法来编码它以单击此按钮?


holdtom
浏览 249回答 3
3回答

湖上湖

所需的元素是一个Angular元素,因此您需要引入WebDriverWait以使该元素可点击,您可以使用以下任一解决方案:cssSelector:new&nbsp;WebDriverWait(driver,&nbsp;20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-default.btn-xs>span.glyphicon.glyphicon-plus[title='Add&nbsp;New&nbsp;Related&nbsp;Number']"))).click();xpath:new&nbsp;WebDriverWait(driver,&nbsp;20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn&nbsp;btn-default&nbsp;btn-xs']/span[@class='glyphicon&nbsp;glyphicon-plus'&nbsp;and&nbsp;@title='Add&nbsp;New&nbsp;Related&nbsp;Number']"))).click();

慕尼黑5688855

你可以尝试这样的事情:driver.findElement(By.xpath("//button[starts-with(@id,'addPiidRelatedNumberBtn_')]")).click();

12345678_0001

您可以使用角度属性,driver.findElement(By.xpath("//button[@ng-click='AddPiidRelatedNumber($event)']"));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java