2015-05-12 36 views
0

我無法識別我正在處於自動化過程中的我們網站上的添加按鈕。 Python,Webdriver。 我試過幾個Xpath建議,它不起作用。我已經與dev交談過,她說這個按鈕位於iFrame之外,頁面上還有其他Add按鈕。我需要找到獨一無二的。我應該使用一個絕對Xpath,並從父項開始,然後繼續工作,直到孩子。我正在努力解決絕對Xpath添加按鈕,這是一個子元素

有沒有人知道Xpath我可以用來做這個請使用Xpath的祖先或兄弟關鍵字?謝謝。

的按鈕,我試圖找到在這裏:

<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;background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAACyklEQVR42l2SeUjTUQDHn62mbmr+kXk08WCpbJmY4p1ppY5Mu4hAE8XQmRVpTbyyPxIzyeaB06ZtHh2baxvqbAZjlorzSE2lQ5MOg1Iw/CPRMPD37adkRd/Hhy883of3eO8RqVS6rarqbnWjXKGSVDf11koejUlqlO9rqjULVZXalYL8BlNiQlHx0ZhLAYQQJtmMTqdjGQwmzM8Dc3PA7OwqpqaWMTGxBJNpEZ2dH1FfP4i8PCUEAmEmrVhviFKpjqXX92I9MzPfqdGhz5TRMAy12oim5g5IalRUpViJa/ky+O6NVNKKN40ZUYlNlo3aUeinVrH49SdkpiZ0Dz6njDojOh60oaVOg/JyDa7kNsPLK6yLlgI3RPZFm8vidgUejy3BOA5EtSbAvykYdfpRRBePw71wBC43RhCUo4crL2JdDKLZQhhJjDhOmQMCFP7gy/3g+dAd5z9Eg1O/B1rDIKrvqJFbrEZqdgs8PEP/ir6nfe3iZScR3OOGtNlDyF84gXtrQpQuJ8Khlo8L6iWQvGnwsg1w9gzX/xFVqmdWiq4e+MtiwG10Ak/JQdG3U9glc0ab4SlKb93E1gZC2UutsT3Q9pM5YQs2RJGohS2p0KFvbAV13dPUPnkEHOu4KG7rg0tJJ0hRA44PcHHGEALiR94QH3KW7m0kJaXRoiiv5a1GOQlN+xcktxZQZQotrl7vgqWUIL7fHRmvIqi0yVDqoJELVi4TxIl4rj+lWWR4yhFRtvydVDIAuaQfhaL7yBLKYFdji4Te/Uh9GUIJX4RRcWo+rM4x1wibHN78QJYBfscS0tMrZpKTbv+Ijc2ajjyQPLQj3HU4LCNqOeiJE2JVPBAv8ppGTMzJbvJPbDgcvsDOzq2EzbYVM5kWheaElWnv4yC3yGHAKpVJEQ9aIsRx43L+C4vGmYb7u3fSx7KnhzfdUfROHvQcY33hL6HaY35wVehRAAAAAElFTkSuQmCC) no-repeat 0px 0px;" src="http://justin-pc.infoshare.local: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> 
+2

(1)清理你的HTML;這是一個畸形的混亂。 (2)在嘗試查找按鈕之前,使用'switchTo()'切換到正確的'iframe';請參閱[這裏](http://stackoverflow.com/q/24474978/290085)。 – kjhughes

+0

我粘貼了Firefox的開發工具HTML。這就是html的方式。開發人員構建它。或者我應該擴展每個標籤並粘貼它?開發人員說這個按鈕不在iFrame中。我不需要切換到它。無論如何,我嘗試切換到它,看看它是否會找到按鈕。它沒有找到按鈕。感謝您的建議。 –

+1

然後,無論你把它粘貼得很差還是開發者都很差(或者兩者都有)。我們不能在給出你發佈的內容的'iframe'遏制方面提供建議,並且如果HTML處於你發佈它的狀態,你就不能指望XPath工作。 – kjhughes

回答

2

我已經設法找到添加按鈕,現在使用下面的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將會中斷。我會暫時使用它,直到我找到更好的方式。
我已經更新了我的問題中的HTML,如果有人可以提供更好的Xpath。

的父類是類=「GWT-TabLayoutPanelTabs」 下一個父類是管理 這是按鈕,它是在管理類

如果我使用下面的XPath我能到管理類,但我不知道如何去按鈕。

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

謝謝。

1

請嘗試以下操作?

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

或只是

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

謝謝!

+0

謝謝,我試過了。我收到錯誤:selenium.common.exceptions.ElementNotVisibleException:消息:無法點擊元素。我想我需要使用Xpath中的祖先,兄弟來找到這個按鈕。這是一個真正的痛苦。 –