如何解决如何在Selenium Web驱动程序C中编写运行时XPATH查询
我试图在div元素内找到span元素的XPath 源代码:
<div class="ohg-patient-banner-suppl-info-section-detail" aria-label="" aria-hidden="false">
<div class="ohg-patient-banner-suppl-info-custom-field">
<span class=" " aria-hidden="true"></span>
<span class=" ohp-metadata-labe">TestLocation</span>
<span class="ohg-patient-banner-suppl-info-custom-row-value-icon " aria-hidden="true"></span>
<span class=" ohg-patient-banner-suppl-info-value">TestLocation Value - </span>
</div>
<div class="ohg-patient-banner-suppl-info-custom-field">
<span class=" " aria-hidden="true"></span>
<span class=" ohp-metadata-label">TestArea</span>
<span class="ohg-patient-banner-suppl-info-custom-row-value-icon fa fa-user-md " aria-hidden="true"></span>
<span class=" ohg-patient-banner-suppl-info-value">TestArea Value</span>
</div>
<div class="ohg-patient-banner-suppl-info-custom-field">
<span class=" " aria-hidden="true"></span>
<span class=" ohp-metadata-label">Testnet</span>
<span class="ohg-patient-banner-suppl-info-custom-row-value-icon " aria-hidden="true"></span>
<span class=" ohg-patient-banner-suppl-info-value">Testnet value</span>
</div>
找到XPath的我的代码:当我编写下面的代码时,我只给了我一个span元素文本,该文本位于第一个div中的TestLocation。
lblBannerVisitNames.Clear();
lblBannerVisitValues.Clear();
IList<IWebElement> VisitFieldsBanner = driver.FindElements(lblAllvisitinfoExpandedMapping);
int NumberofVisitFieldsBanner = VisitFieldsBanner.Count;
for (int i =1; i <= NumberofVisitFieldsBanner; i++)
{
//because we are looping through the list,the xpath query is built at runtime with value i
lblBannerVisitNames.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class='ohp-metadata-label']")).Text);
if (driver.FindElements(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohg-patient-banner-suppl-info-value']")).Count > 0)
lblCurrentVisitFieldValues.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field']/span[@class=' ohg-patient-banner-suppl-info-value']")).Text);
else
lblCurrentVisitFieldValues.Add(ClinicalPortalConstants.NOT_AVAILABLE);
//Add debgugging Later
}
解决方法
我更改了忘记使用“ i”变量值的XPATH
lblBannerVisitNames.Add(driver.FindElement(By.XPath(".//*[@class='ohg-patient-banner-suppl-info-section-detail']/div[@class='ohg-patient-banner-suppl-info-custom-field'][" + i + "]/span[@class=' ohp-metadata-label']")).Text);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。