简体   繁体   English

Selenium Webdriver:JavascriptExecutor推动视频播放

[英]Selenium Webdriver: JavascriptExecutor to push play on video

How to execute play function on page's javascript jquery code using JavascriptExecutor? 如何使用JavascriptExecutor在页面的javascript jQuery代码上执行播放功能?

This is the code from the website: 这是来自网站的代码:

<script type="text/javascript">
jQuery(document).ready(function($) {
$('#wp_mep_1').mediaelementplayer({
m:1
,features: ['playpause','current','progress','duration','volume','tracks','fullscreen']
});
});
</script>

Here is a basic initiation of JavascriptExecutor: 这是JavascriptExecutor的基本启动:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    Object o = js.executeScript("return '123'");

I may be way off, but I feel like I should be setting "o" in this example to $('#wp_mep_1').mediaelementplayer and then passing ("playpause"). 我可能还差得远,但是我觉得在这个例子中我应该将“ o”设置为$('#wp_mep_1')。mediaelementplayer然后传递(“ playpause”)。

Something like: 就像是:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    Object $('#wp_mep_1').mediaelementplayer = js.executeScript('playpause');

I don't have experience with Javascript or jquery, and advice would be most helpful. 我没有使用Javascript或jquery的经验,建议将非常有帮助。

Should be simply: 应该很简单:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("$('#wp_mep_1').play()");

No need to return anything from your JS to your Java, because the .play() is not Selenium API anyway - its part of the JS library used on the page, so wouldn't be able to call it off your object in Java. 无需将任何内容从JS返回到Java,因为.play()仍然不是Selenium API-它是页面上使用的JS库的一部分,因此无法在Java对象中调用它。 You would still need to pass the element to JS. 您仍然需要将元素传递给JS。 (You could select the element in Java first, and then pass to JS for the .play() call like this: (您可以先选择Java中的元素,然后将传递给JS以进行.play()调用,如下所示:

JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement o = driver.findElement(By.id("wp_mep_1"));
js.executeScript("$('arguments[0]').play()", o);

But you might as well select the element in JS too, because it's neater. 但是您也可以在JS中选择元素,因为它更整洁。 (The first way is only 2 lines, while this is 3). (第一种方法只有2行,而这是3行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM