我无法对Selenium Webdriver中的javascript按钮执行点击操作。下面是我的javaScript代码

 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


谁能检查我在哪里做错了?

最佳答案

您可以使用以下任何一种:

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()

10-08 14:55