简体   繁体   中英

Selenium Webdriver: JavascriptExecutor to push play on video

How to execute play function on page's javascript jquery code using JavascriptExecutor?

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 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").

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.

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. You would still need to pass the element to JS. (You could select the element in Java first, and then pass to JS for the .play() call like this:

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. (The first way is only 2 lines, while this is 3).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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