【发布时间】:2021-08-21 21:38:10
【问题描述】:
我有这个 Angular 代码,我想通过自动点击操作进行测试:
<mobileweb-tab-container-item _ngcontent-qoc-c24="" _nghost-qoc-c27="" class="ng-star-inserted">
<!----><!---->
<a _ngcontent-qoc-c27="" href="null" class="ng-star-inserted">
<div _ngcontent-qoc-c27="" class="icon-box" fxlayout="column" fxlayoutalign="space-evently center" style="flex-direction: column; box-sizing: border-box; display: flex; place-content: center flex-start; align-items: center;">
<img _ngcontent-qoc-c27="" class="icon" id="outbound.pick.load.image" src="assets/img/outbound/pick/load.jpg">
<div _ngcontent-qoc-c27="" class="icon-label" id="outbound.pick.load.image.label">Load</div>
</div>
</a>
</mobileweb-tab-container-item>
完整来源:https://pastebin.com/BbKbarPH
我使用此代码自动执行点击操作:
首先选择标签:
WebDriverWait webDriverWait = new WebDriverWait(driver, 25);
new Actions(driver).moveToElement(webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id(tabId)))).click().build().perform();
第二次点击图标:
WebDriverWait webDriverWait = new WebDriverWait(driver, 25);
System.out.println("Click on Icon " + name + " using id locator " + iconId);
WebElement webElement = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id(iconId)));
webElement.click();
问题是有时找不到该元素。例如:
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.id: warehouse.all.accessorial-charge.image (tried for 25 second(s) with 500 milliseconds interval)
at org.mobile.accessorial.charge.AccessorialChargeTest.accessorialChargeTest(AccessorialChargeTest.java:161)
Caused by: org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"css selector","selector":"#warehouse\.all\.accessorial\-charge\.image"}
(Session info: headless chrome=92.0.4515.159)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DTXA-ENVTEST01', ip: '10.100.35.181', os.name: 'Windows Server 2016', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_291'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 92.0.4515.159, chrome: {chromedriverVersion: 92.0.4515.107 (87a818b10553..., userDataDir: C:\Users\devadmin\AppData\L...}, goog:chromeOptions: {debuggerAddress: localhost:54247}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:credBlob: true, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 9c789c51f703a81f1d1ac2b0ac792bba
*** Element info: {Using=id, value=warehouse.all.accessorial-charge.image}
at org.mobile.accessorial.charge.AccessorialChargeTest.accessorialChargeTest(AccessorialChargeTest.java:161)
在标签加载期间,有一个动画来加载标签和图标。是否有其他方法可以实现等待组件完全加载并执行点击操作的侦听器?
【问题讨论】:
-
您的标题不是问题,而是部分错误消息,没有多大帮助。此外,您的标题中的错误消息是您提到该错误消息的唯一地方,并且您在问题中提到了 不同 错误消息。您当前的代码收到了哪个错误消息?你永远不会指出你试图点击哪个元素。你没有显示你的定位器,你只是显示变量名而不是赋值。当您所做的只是单击时,为什么在您的第一个代码块中使用
Actions?只需使用与第二次点击相同的代码WebDriverWait。 -
您需要澄清和简化您的问题。请参阅 minimal reproducible example 和 help center。请编辑您的问题并将焦点缩小到您遇到的实际问题......清楚地描述。然后添加您正在使用的导致错误的代码,清楚地指出是哪一行导致了错误。
标签: java selenium selenium-webdriver selenium-chromedriver