我正在尝试计算绝对 Xpath 一个作为子元素的添加按钮

I am trying to work out absolute Xpath an Add button that is a child element

我在识别我们网站上的“添加”按钮时遇到了问题,我正在自动执行该按钮。 Python, 网络驱动程序。 我尝试了一些 Xpath 建议,但它不起作用。我已经和开发人员谈过了,她说按钮在 iFrame 之外,页面上还有其他添加按钮。我需要找到独一无二的。我应该使用 Absolute Xpath,从父级开始,一直到子级。

有谁知道我可以使用什么 Xpath 来执行此操作,请使用 Xpath 的 Ancestor 或 sibling 关键字?谢谢

我要查找的按钮在这里:

<div class="gwt-HTML" style="margin-left: 15px;">Add...</div>

HTML如下(为了方便大家阅读,我将其缩短)

root
<div class="gwt-TabLayoutPanelTabs" style="position: absolute; left: 0px; right: 0px; bottom: 0px; width: 16384px;">  <!-- this is the parent -->
|   <...>
|   |   <div class="gwt-HTML">Operations</div>
|   |   <div class="gwt-HTML">Administration</div>  <!-- The Add button is in the same position as this div -->
|   </div>
|   <div>
|   |   <div class="gwt-TabLayoutPanelContentContainer" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
|   |   |   <div></div>
|   |   |   <div></div>
|   |   |   <div></div>
|   |   |   <div></div>
|   |   |   <div></div>
|   |   |   <div></div>
|   |   |   <div>
|   |   |   |   <!--I need this one.  This div is at index 7 which has the Add button-->
|   |   |   |   <button class="gwt-Button" type="button">
|   |   |   |       <div style="position: absolute; margin-left: 2px;">
|   |   |   |       <div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
|   |   |   |   </button>
|   |   |   |   <button class="gwt-Button" type="button" disabled="">
|   |   |   |   <...>
|   |   |   </div>
|   |   </div>
|   </div>
</div>

完整的HTML如下:

    <html style="overflow: hidden;">
<head>
<body style="margin: 0px;">
<iframe id="__gwt_historyFrame" style="position: absolute; width: 0; height: 0; border: 0;" tabindex="-1" src="javascript:''"/>
<noscript> <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif;"> Your web browser must have JavaScript enabled in order for this application to display correctly.</div> </noscript>
<script src="spinner.js" type="text/javascript"/>
<script type="text/javascript">   cImageSrc = 'images/sprites.gif';   new imageLoader(cImageSrc, 'startAnimation()');       var info = {      "cc4Address" : "https://riaz-pc.company.local:8889/ClearCore",      "rrdAddress" : "https://riaz-pc.company.local:8889/ClearCoreRRD",      "gazAddress" : "https://riaz-pc.company.local:8889/Gazetteer",         "debugEngine" : "null",         "secure" : "true",         "suppressErrors" : "true",         "testing" : "null",         "serverName" : "riaz-pc.company.local"         };       var timeout = {         "timeoutAlerts" : "300000",         "timeoutDefault" : "5000",         "timeoutLogout" : "3600000",         "timeoutProgress" : "5000",         "timeoutSoap" : "300000",         "timeoutClearDb" : "3600000"         };       var cache = {         "cacheMax" : "500",         "pageSize" : "25",         "startTrig" : "50",         "endTrig" : "450",         "maxUsnSize" :                 "1000",         "defaultPageSize" : "500"         };       var ignoreVersion = {       "major" : "true",         "minor" : "true",         "incremental" : "true"       };   var guiVersion = {              "major" : "5",     "minor" : "1",     "incremental" : "1",     "build" : "5565"     };    </script>
<script src="ClearCore/ClearCore.nocache.js" type="text/javascript"/>
<script defer="defer">ClearCore.onInjectionDone('ClearCore')</script>
<iframe id="ClearCore" src="javascript:''" style="position: absolute; width: 0px; height: 0px; border: medium none;" tabindex="-1"/>
<div style="position: absolute; z-index: -32767; top: -20cm; width: 10cm; height: 10cm; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 1px; top: 1px; right: 1px; bottom: 1px;">
<div class="gwt-TabLayoutPanel" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 30px;">
<div class="gwt-TabLayoutPanelTabs" style="position: absolute; left: 0px; right: 0px; bottom: 0px; width: 16384px;">
<div class="gwt-TabLayoutPanelTab GAT4PNUK gwt-TabLayoutPanelTab-selected" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTabInner">
<div class="gwt-HTML">Operations</div>
</div>
</div>
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);">
<div class="gwt-TabLayoutPanelTabInner">
<div class="gwt-HTML">Administration</div>
</div>
</div>
</div>
</div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 30px; right: 0px; bottom: 0px;">
<div class="gwt-TabLayoutPanelContentContainer" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0%; top: 0%; width: 100%; height: 100%;">
<div class="gwt-TabLayoutPanelContent" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;" aria-hidden="false">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 35px;">
<div class="GAT4PNUDP GAT4PNUKN" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div class="GAT4PNUGP">
<div class="GAT4PNUCP">
<div>
<select class="gwt-ListBox GAT4PNUEP" style="left: 445px;">
<option value="More tools...">More tools...</option>
<option value="Statistics">Statistics</option>
<option value="Snapshot Trending">Snapshot Trending</option>
<option value="Task Statistics">Task Statistics</option>
<option value="User Alerts">User Alerts</option>
</select>
</div>
</div>
</div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 35px; right: 0px; bottom: 0px;">
<div class="gwt-SplitLayoutPanel" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; bottom: 0px; width: 160px;">
<div style="overflow: auto; position: absolute; background-color: rgb(254, 255, 238); left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: relative;">
<div>
<select class="gwt-ListBox" style="width: 100%;">
<div class="GAT4PNUDG" __gwtcellbasedwidgetimpldispatchingfocus="true" __gwtcellbasedwidgetimpldispatchingblur="true" role="tree">
<div style="overflow: hidden;">
<div>
<div>
<div aria-selected="false" role="treeitem" aria-setsize="1" aria-posinset="1" aria-expanded="true" aria-level="1">
<div class="GAT4PNUIF GAT4PNUAG" style="padding-left: 0px;">
<div style="overflow: hidden;">
<div>
<div>
<div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="1" aria-expanded="false" aria-level="2">
<div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="2" aria-expanded="false" aria-level="2">
<div aria-selected="false" role="treeitem" aria-setsize="3" aria-posinset="3" aria-expanded="false" aria-level="2">
<div class="GAT4PNUIF" style="padding-left: 16px;">
<div class="GAT4PNUIF GAT4PNUKF" style="padding-left: 16px;position:relative;" onclick="">
<div class="GAT4PNUJF" style="left: 0px;width: 15px;height: 15px;position:absolute;">
<div class="GAT4PNULF">
<div style="padding-left: 22px;position:relative;zoom:1;">
<div style="left:0px;margin-top:-8px;position:absolute;top:50%;line-height:0px;">
<div>
<span class="" title="Tasks" style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;empty-cells:show;display:block;">Tasks</span>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="GAT4PNUHF" style="display: none;">Empty</div>
<a class="GAT4PNUPF" href="javascript:;" style="display: none;">Show more</a>
</div>
</div>
</div>
</div>
<div class="GAT4PNUHF" style="display: none;" tabindex="0">Empty</div>
<a class="GAT4PNUPF" href="javascript:;" style="display: none;">Show more</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="position: absolute; overflow: hidden; left: 160px; top: 0px; bottom: 0px; width: 8px;">
<div class="gwt-SplitLayoutPanel-HDragger" style="width: 8px; position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;"/>
</div>
<div style="position: absolute; overflow: hidden; left: 168px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"/>
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 24px;">
<div class="workspacetoolbar" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div>
<div style="position: absolute; top: 3px; left: 5px;">
<span class="gwt-InlineLabel" style="font-weight: bold; color: black; margin-right: 5px; margin-left: 20px;">Reports</span>
<div class="GAT4PNUMP">
<button class="gwt-Button" type="button">
<div style="position: absolute; margin-left: 2px;">
<img border="0" style="width:14px;height:14px;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8080/clearcore501/ClearCore/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";"/>
</div>
<div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
</button>
<button class="gwt-Button" type="button" disabled="">
<button class="gwt-Button" type="button" disabled="">
<div class="GAT4PNUMP">
<button class="gwt-Button" type="button" title="Run the selected reports" disabled="">
<button class="gwt-Button" type="button" title="View the selected report" disabled="">
<button class="gwt-Button workspacebuttontext130" type="button" title="Conditional report" disabled="">Conditional report ▼</button>
<div class="GAT4PNUMP">
<select class="gwt-ListBox" style="height: 20px; margin-right: 5px;" disabled="">
</div>
<img class="gwt-Image helplink pointer" border="0" style="width:16px;height:16px;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAADAElEQVR42m1TW0iTYRieE7tUB17ajRBFeBOUYaBoTtG8ULQMxKR0HgjBoVJh4CEtPPw6IUHKQ2o1nCPNAwax2WZzrjyl8zCn/puHf3PN4zZSEnv6tv6WlQ88Nx/f877f877Px+Ecw+LiordSqSxQq9XKifFxRjMywnR3dw3V19cXRkVFnSZXThF6cE4CwzD8ublZ88E3Bxy2XRhMVhgYK2w7W7CYGUgkEnNOTk4yuepDyP1PbKBporWj5YMRD97ZIexzIEO6i+R2C2r6l7Bl3YC0s/MgLi4ui0j83EXGxsZ8ZrRas8NuAyXbhEj9A5TqEKWyfeT37iFDso34ZgvutBpgMa2jhqJMPB4vln2JB0culxc4bHt4oVhBjeoIAwvfsbZziN+wHxyha3IX4U9XUdGjh3ZqAqGhoQ1EHEjoxVEoFMrtTSvuD9jxRG6D0boP3foOUit7kVDyxl0osm4BISIjVmk9UlJSNEScQMjjDKtUjH7F5PJc2GPF3QY10urkyG6ZxtzaHkYXzMhtUCCsSovAChpTszoIhbkMEecR+nNkMhmjN5ggkO4hr2cbQimDnNdGd+fwex0IKx1ESLUO58ppTEzNIjs701ngEWEARywWDzmHk/zqKwQdm8js3MLtthVcK+l3iSMrRhEhWsLlqmUEUUbQizpER0d/chegKKqQXtajum8B8S0W3GzdQOKzPy+IqNXjSg2Ns2VLKJHOQERVIiAgQOy2EBMT49/U2GheNS4j7eUKrtavg187j6D8t7gg7MbFsmmX+MZzA1RKGZKSkkyenp7N7iE6V5Genp7c3NR0oNfNorxrHqEiA84XfcGZh5O4VE2jWKLF0OB7ZAgE+yQDbURT5F4jm20f4iurpKjIJG5vxdS4BiOfJzCsGYX64yAelxa7Ovv6+jrFVYSR7iCxcMbSz9vbOzY4OLjhemKiRiAQrKWm3lrj8/kap2cul9vEdo78K8rHwGWrBrL+nEMqZZnHngWe+JmOwYP1xXNN2LmmX/Rnz7z+/c4/ARDk76ne92ZwAAAAAElFTkSuQmCC) no-repeat 0px 0px;" src="http://justin-pc.infoshare.local:8080/clearcore501/ClearCore/clear.cache.gif" onload="this.__gwtLastUnhandledEvent="load";"/>
</div>
</div>
<div style="position: absolute; overflow: hidden; left: 0px; top: 24px; right: 0px; height: 24px;">
<div style="position: absolute; overflow: hidden; left: 0px; top: 48px; right: 0px; bottom: 0px;">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
<div style="position: absolute; overflow: hidden; display: none; left: 0px; top: 0px; right: 0px; bottom: 0px;">
</div>
</div>
</div>
</div>
<div style="position: absolute; overflow: hidden; top: 1px; right: 1px; width: 30px; height: 25px;">
<div style="position: absolute; overflow: hidden; left: 0px; top: -25px; right: 0px; height: 25px;">
</div>
</div>
</div>
<div style="display: none;" aria-hidden="true"/>
</body>
</html>

尝试以下方法?

//button[@class='gwt-Button']/div[text()='Add...']

或者只是

//div[text()='Add...']

谢谢!

我已经设法找到“添加”按钮并使用以下 Xpath 现在单击它。

driver.find_element_by_xpath("/html/body/div[2]/div[2]/div/div[2]/div/div[3]/div/div[8]/div/div[3]/div/div[4]/div/div[2]/div/div[2]/div/div/button[1]")

我知道这不是最好的方法,因为如果结构发生变化,Xpath 就会中断。在找到更好的方法之前,我将暂时使用它。
如果有人可以提供更好的 Xpath,我已经更新了问题中的 HTML。

父 class 是 class='gwt-TabLayoutPanelTabs' 下一个父级 class 是 Administration 这是按钮所在的位置,它在管理 class

如果我使用以下 Xpath,我可以进入管理 class 但我不知道如何进入该按钮。

//div[@class='gwt-TabLayoutPanelTabs']/div[7]

谢谢。