我在页面上动态弹出一个微调器图标,指示页面正在加载,我需要检测它何时消失(这一点对我来说很清楚)
每次我在google chrome inspect中单击此微调器,然后按copy xpath时,都会得到以下提示:
/html/body/vr-root/vr-route-handler/vr-layout/mat-sidenav-container/mat-sidenav-content/div/mat-sidenav-container/mat-sidenav-content/div/div/bs-campus/div/div/bs-data-grid/div/div[2]/mat-spinner/svg/circle
但是当我尝试定位此元素时(即使在Chrome检查工具中,按下CTR + F也找不到它。
有没有其他方法可以找到它?我试图用相对xpath搜索它,但是它也失败了:
.//div/mat-spinner
编辑:添加图片
编辑2:当我在Chrome Inspect中暂停页面->源->暂停脚本执行时,我可以通过以下方式在chrome inspect中搜索并找到(ctr + f)元素:
.//mat-spinner
但是当页面正在运行(并且元素仍然可见)时,无法找到它。
尝试使用网络驱动程序访问错误日志
org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.xpath: .//div/mat-spinner (tried for 10 second(s) with 500 milliseconds interval)
at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:113)
at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:283)
at base.StaticSeleniumDriver.getWebElement(StaticSeleniumDriver.java:362)
at steps.BaseSteps.debugDrawSpinner(BaseSteps.java:55)
at ?.Given Debug draw spinner(Campus.feature:190)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: .//div/mat-spinner
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.14.0', revision: 'aacccce0', time: '2018-08-02T20:19:58.91Z'
System info: host: 'BANNB061', ip: '10.0.75.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_181'
Driver info: driver.version: StaticSeleniumDriver
使用以下代码:
WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
return wait.until(ExpectedConditions.elementToBeClickable(By.xpath(xpath)));
答案 0 :(得分:1)
这是我在量角器中提出的解决方案。 我的逻辑是在与网站互动后检查微调框是否存在。 例如:点击元素,输入一些文字,页面导航..弹出窗口等。 您可以找到放置它的正确位置。 希望对您有所帮助,您可以将其适应任何语言。硒API很常见。
请注意,如果您找到 .// mat-spinner 用作元素的xpath。
export async function handleSpinner() {
console.log('Checking if loading spinner is present on the page.');
let spinner = element(by.css('div.spinner'));
try {
// setting minimal timeout to search for the element
await browser.manage().timeouts().implicitlyWait(500);
// trigger check if there is such element on the page. will throw exception if not present
await spinner.getWebElement();
console.log('Spinner has been found.. waiting.... up to 10 seconds.');
await browser.wait(ExpectedConditions.invisibilityOf(spinner), 10000, 'Spinner is still present...');
console.log('Spinner has disappeared.');
} catch (ex) {
// spiner is not present so ignore the exception
} finally {
// Setting browser implicit timeout back to the original configuration.
await browser.manage().timeouts().implicitlyWait(10000);
}
}