如何使用JavaScript单击Selenium WebDriver中的元素

我有以下HTML:


<button name="btnG" class="gbqfb" aria-label="Google Search" id="gbqfb"><span class="gbqfi"></span></button>

我下面的单击“ Google搜索”按钮的代码在WebDriver中使用Java时效果很好。


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

我想将JavaScript与WebDriver 一起使用以单击按钮。我该怎么做?


九州编程
浏览 1414回答 3
3回答

波斯汪

我知道这不是JavaScript,但是您实际上也可以使用鼠标单击来单击动态Javascript锚点:public static void mouseClickByLocator( String cssLocator ) {&nbsp; &nbsp; &nbsp;String locator = cssLocator;&nbsp; &nbsp; &nbsp;WebElement el = driver.findElement( By.cssSelector( locator ) );&nbsp; &nbsp; &nbsp;Actions builder = new Actions(driver);&nbsp; &nbsp; &nbsp;builder.moveToElement( el ).click( el );&nbsp; &nbsp; &nbsp;builder.perform();}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
JavaScript