无法通过xpath选择和单击链接锚标记,它返回一个包含1个元素的数组

时间:2018-08-03 10:31:49

标签: java selenium-webdriver xpath

<a href="MappingAndEnrichmentSetUp.aspx" title="Mapping &amp; Enrichment" class="rpLink rpExpandable  rpSelected" target="ctl00_DefaultContent_radPaneProjectMaintContent">
    <span class="rpOut rpNavigation">
        <span class=""></span>
        <span class="rpText" style="">Mapping &amp; Enrichment</span>
    </span>
</a>

这是我从控制台获得的信息

a=$x("//a[@title='Mapping & Enrichment']")

输出:

[a.rpLink.rpExpandable] 

在索引0处具有链接和隐藏的照片元素

a[0].click()  --works fine

a-返回数组

是否可以使用xpath直接获取上述html代码段的锚标记?

我在java testNG类中使用以下代码来单击链接。它不起作用。

控制台调试:

a=$x("(//a[@href='ProjectDimensionSetUp.aspx'])")
[a.rpLink]
  0: a.rpLinklength: 
  1__proto__: Array(0)

a=$x("(//a[@href='ProjectDimensionSetUp.aspx'])")[0]
<a href=​"ProjectDimensionSetUp.aspx" title=​"Project Dimensions" class=​"rpLink" target=​"ctl00_DefaultContent_radPaneProjectMaintContent">​…​</a>​

是否可以通过xpath直接选择元素? 这样,我可以使用testNG Java WebElement中的a.click()

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("XPATH_TO_SELECT_ANCHOR_TAG_NOT_ARRAY_WITH_ANCHOR_TAG"))).click();

Java控制台日志: Java Log TestNG

JavaScript warning: http://XX.XX.XXX.XXX/XXXXX/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=DefaultMasterScriptManager_TSM&compress=1&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3aen-US%3a4ae4914f-8c8e-4123-9d05-414d87b48357%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2014.1.225.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3aen-US%3afe3df733-ee56-4563-8789-bc399360084a%3a16e4e7cd%3aed16cbdc%3af7645509%3a88144a7a%3a24ee1bba%3a92fe8ea0%3afa31b949%3a19620875%3a874f8ea2%3af46195d3%3a490a9d4e%3abd8f85e4%3a7165f74%3a1e771326%3aa7e79140%3a2003d0b8%3aaa288e2d%3a258f1c72%3a58366029%3ab7778d6c%3ae085fe68%3ae330518b%3ac128760b%3ac8618e41%3ae4f8f289%3a1a73651d%3a333f8d94, line 2030: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript warning: http://XX.XX.XXX.XXX/XXXXX/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=DefaultMasterScriptManager_TSM&compress=1&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3aen-US%3a4ae4914f-8c8e-4123-9d05-414d87b48357%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2014.1.225.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3aen-US%3afe3df733-ee56-4563-8789-bc399360084a%3a16e4e7cd%3aed16cbdc%3af7645509%3a88144a7a%3a24ee1bba%3ae330518b%3a2003d0b8%3ac128760b%3a1e771326%3ac8618e41%3a1a73651d%3a333f8d94%3a11a04f7e%3af46195d3%3a4cacbc31%3aaa288e2d%3a258f1c72%3a7165f74%3a874f8ea2, line 2030: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
JavaScript warning: http://IP_XXXXXXXX/XXXXXX/Telerik.Web.UI.WebResource.axd?_TSM_HiddenField_=DefaultMasterScriptManager_TSM&compress=1&_TSM_CombinedScripts_=%3b%3bSystem.Web.Extensions%2c+Version%3d4.0.0.0%2c+Culture%3dneutral%2c+PublicKeyToken%3d31bf3856ad364e35%3aen-US%3a4ae4914f-8c8e-4123-9d05-414d87b48357%3aea597d4b%3ab25378d2%3bTelerik.Web.UI%2c+Version%3d2014.1.225.40%2c+Culture%3dneutral%2c+PublicKeyToken%3d121fae78165ba3d4%3aen-US%3afe3df733-ee56-4563-8789-bc399360084a%3a16e4e7cd%3aed16cbdc%3af7645509%3a88144a7a%3a24ee1bba%3a92fe8ea0%3afa31b949%3a19620875%3a874f8ea2%3af46195d3%3a490a9d4e%3abd8f85e4%3a7165f74%3ab7778d6c%3a2003d0b8%3a1e771326%3aaa288e2d%3a258f1c72%3a8674cba1%3a7c926187%3a2e42e72a%3aa51ee93e%3aef347303%3ac08e9f8a%3a2bef5fcc%3ae06b58fd%3ae085fe68%3aa9b7ace7, line 2030: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead
[Parent 19988, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Parent 19988, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 6324, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 6324, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_chann[Cehildl_win. 19cc, lin648, Chrome_Ce 346
hildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Parent 19988, Gecko_IOThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 19204, Chrome_ChildThread] WARNING: pipe error: 109: file z:/build/build/src/ipc/chromium/src/chrome/common/ipc_channel_win.cc, line 346
[Child 19204, Chrome_ChildThread] *** UTM:SVC TimerManager:registerTimer called after profile-before-change notification. Ignoring timer registration for id: telemetry_modules_ping
[GPU 19676, Chrome_ChildThread] WARNIN
###!!! [Child][MessageChannel::SendAndWait] Error: Channel error: cannot send/recv

2 个答案:

答案 0 :(得分:1)

根据您提供的HTML来调用 link 上的click(),文本为 Mapping&Enrichment (映射和丰富化),您需要诱导 WebDriverWait 以使所需的元素可点击,您可以使用以下解决方案:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@class='rpLink rpExpandable  rpSelected' and @href='MappingAndEnrichmentSetUp.aspx']/span[@class='rpOut rpNavigation']//span[@class='rpText']"))).click();

答案 1 :(得分:-3)

从我们在评论中的对话看来,您似乎在混淆两件事。

在浏览器控制台中,执行$x()返回一个元素集合,因此$x().click()不起作用,因为您无法单击整个集合。如果使用数组符号,则可以指定要单击的集合的成员,例如$x()[0].click()将单击集合中的第一个元素。

要将其转换为Java / Selenium,driver.findElements()(复数)与$x()等效,因为它们都返回元素的集合。如果使用driver.findElement()(单数),则即使位于多个元素中,它也仅返回第一个元素。因此,即使使用的定位器找到了多个元素,您也可以安全地使用driver.findElement().click()

如果您想使用返回多个元素但不希望仅获取第一个元素的定位符,则可以使用.get(x),其中x是您想要的元素在索引中的索引采集。例如,driver.findElements().get(1)将返回 second 元素(0是第一个元素的索引,1是第二个元素的索引,依此类推)。