2016-03-04 91 views
1

我無法對按鈕thro'在selenium Webdriver中執行點擊操作。下面是我的javascript代碼Javascript在Java中使用Selenium Webdriver

JavascriptExecutor js=(JavascriptExecutor) driver; 
      // js.executeScript("document.getElementById('customerid').click();"); 

      js.executeScript("document.getElementsByClassName('ms-crm-ImageStrip-btn_off_lookup ms-crm-Lookup').click();"); 

下面是我的HTML代碼

td class="Lookup_RenderButton_td" style="width: 21px"> 
<img id="customerid" class="ms-crm-ImageStrip-btn_off_lookup ms-crm-Lookup" defaultviewid="{A9AF0AB8-861D-4CFA-92A5-C6281FED7FAB}" savedquerytype="" isdisplayonly="false" resolveemailaddress="0" showproperty="1" disableviewpicker="0" disablequickfind="0" disablemru="0" allowfilteroff="1" autoresolve="1" defaulttype="1" lookupstyle="single" lookupbrowse="0" lookuptypeicons="/_imgs/ico_16_1.gif?ver=-1567689440:/_imgs/ico_16_2.gif?ver=-1567689440" lookuptypenames="account:1:Account,contact:2:Contact" crmattributeid="{09d25a7a-420f-42f7-bad4-192edc51356a}" lookuptypes="1,2" attrpriv="7" attrname="customerid" style="ime-mode:auto" req="2" alt="Click to select a value for Customer Name." src="/_imgs/btn_on_lookup.png" title="Click to select a value for Customer Name." forcesubmit="false"/> 
<a tabindex="-1" onclick="Mscrm.Utilities.click(previousSibling);" href="#" title="Click to select a value for Customer Name."/> 
</td> 

異常我越來越:

Exception in thread "main" org.openqa.selenium.WebDriverException: document.getElementsByClassName(...).click is not a function 
Command duration or timeout: 12 milliseconds 

任何人都可以查看我在哪裏做的錯誤?

回答

1

document.getElementsByClassName()將返回元素列表,而不是元素。

沒有測試,嘗試

js.executeScript("document.getElementsByClassName('ms-crm-ImageStrip-btn_off_lookup ms-crm-Lookup')[0].click();"); 

(調用getElementsByClassName功能之後添加[0]

+0

感謝。它正在工作 –

1

你可以使用任何一個:

driver.execute_script("document.getElementsByClassName('ms-crm-ImageStrip-btn_off_lookup ms-crm-Lookup')[0].click();") 

content = driver.find_element_by_class_name('ms-crm-ImageStrip-btn_off_lookup ms-crm-Lookup') 
content.click() 
+0

謝謝..它的作品 –

相關問題