2015-05-12 64 views
1

我無法確定我正在處於自動化過程中的網站上的添加按鈕。 Python,Webdriver。 硒IDE提供了以下XPath:我正在嘗試爲Xpath或CSS添加按鈕

adminAdd_button = driver.find_element_by_xpath("//button[@type='button'])[33]") 
adminAdd_button.click() 

我收到以下錯誤,當我在我的蟒蛇,webdriver的代碼試試這個XPath。

selenium.common.exceptions.InvalidSelectorException: Message: The xpath expression '//button[@type='button'])[33]' cannot be evaluated or does notresult in a WebElement 

我也曾嘗試下面的XPath沒有運氣:

driver.find_element_by_xpath("//div[. = 'Add']") 

有誰知道的Xpath我可以用這個嗎?或CSS作爲CSS比Xpath更快執行。謝謝。

HTML源代碼如下所示(Add位於源代碼的底部)。有這麼多的div標籤:

<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"> 
<script src="ClearCore/ClearCore.nocache.js" type="text/javascript"> 
<script defer="defer"> 
<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> 
<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> 
<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> 
<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> 
<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" style="background-color: rgb(254, 255, 238);"> 
<div class="gwt-TabLayoutPanelTabInner"> 
</div> 
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);"> 
<div class="gwt-TabLayoutPanelTabInner"> 
</div> 
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);"> 
<div class="gwt-TabLayoutPanelTabInner"> 
</div> 
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);"> 
<div class="gwt-TabLayoutPanelTabInner"> 
</div> 
<div class="gwt-TabLayoutPanelTab GAT4PNUK" style="background-color: rgb(254, 255, 238);"> 
<div class="gwt-TabLayoutPanelTabInner"> 
</div> 
<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 gwt-TabLayoutPanelTab-selected" 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> 
<div style="position: absolute; overflow: hidden; left: -100%; top: 0%; width: 100%; height: 100%; display: none;"> 
<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; left: 100%; top: 0%; width: 100%; height: 100%; display: none;"> 
<div class="gwt-TabLayoutPanelContent" style="position: absolute; left: 0px; top: 0px; right: 0px; bottom: 0px; display: none;" aria-hidden="true"> 
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 35px;"> 
<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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; bottom: 0px; width: 160px;"> 
<div style="position: absolute; overflow: hidden; left: 160px; top: 0px; bottom: 0px; width: 8px;"> 
<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> 
<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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 24px;"> 
<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 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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 30px;"> 
<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> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
</div> 
<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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; height: 35px;"> 
<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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; bottom: 0px; width: 160px;"> 
<div style="position: absolute; overflow: hidden; left: 160px; top: 0px; bottom: 0px; width: 8px;"> 
<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> 
<div style="position: absolute; overflow: hidden; left: 0px; top: 0px; right: 0px; bottom: 0px;"> 
<div style="right: 0px; bottom: 0px; position: absolute; left: 0px; top: 0px;"> 
<div style="position: absolute; z-index: -32767; top: -20ex; width: 10em; height: 10ex; visibility: hidden;" aria-hidden="true"> </div> 
<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;">Projects</span> 
<div class="GAT4PNUMP"> 
<button class="gwt-Button" type="button"> 
<div style="position: absolute; margin-left: 2px;"> 
<div id="prueba" class="gwt-HTML" style="margin-left: 15px;">Add...</div> 
</button> 
<button class="gwt-Button" type="button" disabled=""> 
<button class="gwt-Button workspacebuttontext75" type="button" title="Delete" disabled="">Delete ▼</button> 
<div class="GAT4PNUMP"> 
+0

如果你有* *類爲什麼不只是做:'find_element_by_xpath(「//按鈕[@類=‘GWT-按鈕’] 「)'? – Anzel

+0

,但確保您*實際*切換到iframe。 – Anzel

+0

在選擇器中使用索引是不好的做法,恕我直言。如果您提供了更多所需的div內容,我可以嘗試爲您構建一個有效的css選擇器 –

回答

1

<div>並非只有「添加」,在它的文字,請嘗試使用starts-with()代替:

//div[starts-with(., 'Add')] 

按鈕未很好形成-IT沒有相應的結束標記,因此很難確定瀏覽器如何解釋它。假設div文本Add...解釋爲按鈕孩子,這也應該工作:

//button[starts-with(., 'Add')] 
+0

謝謝 - 我可以使用您的Xpath在FirePath中找到該按鈕。當我運行我的代碼時,它找不到它。我剛剛與開發人員交談過,她說添加按鈕不是唯一的。頁面上還有其他添加按鈕。我需要找到父類,然後找到具有添加按鈕的子項。該按鈕位於「管理」類中,例如

Administration
我在上面的問題中粘貼了完整的html –

+0

開發人員說她可以構建一個JQuery來通過CSS選擇按鈕。我可以在Selenium Webdriver中使用JQuery嗎?那麼我可以使用她的JQuey。 –

+0

@RiazLadhani假設她使用CSS選擇器和JQuery來選擇按鈕,那麼您可以在Selenium中使用相同的選擇器。請參閱:http://selenium-python.readthedocs.org/en/latest/locating-elements.html#locating-elements-by-css-selectors – har07

0

首先,你得到,因爲在XPath表達式額外的右括號的InvalidSelectorException例外:

//button[@type='button'])[33] 
        HERE^ 

driver.find_element_by_xpath("//div[. = 'Add']")方法是行不通的,因爲你問的div元素的文字正好等於Add這是不正確的,文字是Add...

//div[. = 'Add...'] 

而且,根據發佈的HTML代碼,所期望的div元素不是iframe內部,因此,沒有必要切換。

另一個重要的事情要注意:你確定這個div元素是可點擊的嗎?這可能是因爲父button元素是你要的東西點擊:

//button[div[. = 'Add...']] 
相關問題