我正在尝试为以下代码找到xpath。但是它显示出一些错误。
检查元素是:

<BUTTON title="Create Customer" class="SBCUSTMENU awbButton" id=C2_W10_V11_btnCREATE_CUSTMC onmouseout=javacript:awbImgButtonMouseOut(this); onmousedown=javacript:awbImgButtonMouseDown(this); onmouseup=javacript:awbImgButtonMouseUp(this); onclick="javascript:if(awbBtnActive(this)){if (awbAddMonitorIdsToHttp(['MONITOR_JOURNEY','','MONITOR_STEP',''])){htmlbSubmitLib('htmlb',this,'htmlb:button:click:null','myFormId','C2_W10_V11_btnCREATE_CUSTMC','onclick_CREATE_CUSTMC',0);}}return false;" type=button awbClass="awbButton SBCUSTMENU">&nbsp;</BUTTON>

driver.findElement(By.id("C2_W10_V11_btnCREATE_CUSTMC")).click();


我期待以下错误

Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for visibility of element located by By.id: C2_W10_V11_btnCREATE_CUSTMC
Build info: version: '2.53.1', revision: 'a36b8b1', time: '2016-06-30 17:37:03'
System info: host: 'PC404441', ip: '10.27.101.9', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_192'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:10768/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]

最佳答案

如果您单击的元素是动态生成的,则可能需要引发WebDriverWait:

import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

(new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[title='Create Customer']")).click();

10-08 17:07