2015-08-20 49 views
2

目前即時通訊嘗試自動填充一個相當大的網絡形式與硒和Java。即時通訊使用Firefox的webdriver。我安裝了firebug和xpath檢查器(在Firefox上)。Selenium webdriver識別按鈕並點擊它使用Java

我的問題是登錄到網站後點擊按鈕。登錄部分沒有問題。它的用戶名/密碼並提交操作。

我嘗試了各種FindElement方法,我認爲它應該是Xpath。我根本不知道如何識別按鈕,因爲它沒有名稱屬性。

XPath的檢查:ID( 'sectiondiv_81')/ X:輸入

<div id="sectiondiv_81" class="ax-boxcontent" style=""> 
 
<input class="Button" type="button" onclick="window.location='/Apps/app_editopportunity.jsp?appid=103184&nextlevel=1&companyid=&opportunityid=0&opportunitytypeid=13997&defaultstatusid=12824&providertoken=Sml3L21uJDk!HkITNmAzGDUlHhd2GxoXH2JcU0JFal9AQVxWHwQIUxkbbFFfXhI~'" value="Apply for a scientific research project"> 
 
<br> 
 
</div>

它涉及與價值的按鈕 '申請一個科研項目'。

我非常感謝解決方案。

回答

0

如果是裏面唯一的按鈕,你也許能找到它與XPath,如:

("//div[contains(@id, 'sectiondiv_81')]/input[contains(@class, 'Button')]") 

("//input[contains(@value, 'scientific research project')]") 
+0

歡呼的回覆。我得到這個錯誤:線程「main」中的異常org.openqa.selenium.NoSuchElementException:無法找到元素:{「method」:「xpath」,「selector」:「// div [contains(@id,'sectiondiv_81' )]/input [contains(@class,'Button')]「} 我使用的代碼:driver.findElement(By.xpath(」// div [contains(@id,'sectiondiv_81')]/input [contains (@class,'Button')]「))。click(); –

+0

這可能是因爲具有相同Xpath的多個元素。試用價值搜索方法,我已經更新了我的答案.. –

0

我們可以看看完整的網站分析呢?

我建議你螢火和Firepath檢查像與XPath:

//div[contains(@id, 'sectiondiv_81')]/input[contains(@class, 'Button')] 

,你將能夠檢查與您的XPath相符的網站上的「節」或「部分」

您也可以使用Firebug獲得XPath,右鍵單擊 - >檢查FirePath

0

您應該避免使用數字,索引(如果可能),因爲它會使xpath查詢對於可能的UI更改不靈活。 最好使用任何元素屬性,這會給你獨特的結果。

//input[@type='button'][contains(@value, 'Apply')] 
0

<input>元素是<div>元件的與ID同級所以嘗試使用XPath以下-同級命令像這樣:

WebElement myButton = driver.findElement(By.xpath("//div[@id='sectiondiv_81']/following-sibling::input")); 
myButton.click(); 

這來自W3C XPath spec

相關問題