Java Python Ruby
执行JavaScript代码 JavascriptExecutor jse= (JavascriptExecutor)driver;
String jsContent= "JavaScript代码";
jse.executeScript(jsContent);
driver.execute_script(script, *args) driver.execute_script(script, *args)

移动窗体滚动条

window.scrollTo(左边距,右边距) 方法可以设置浏览器窗口滚动条的水平和垂直位置。

Java

package JavaTest;

import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
    public static void main(String[] arg) throws InterruptedException, IOException
    {
        WebDriver driver = new FirefoxDriver();

         // 设置隐示等待时长:10秒;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.baidu.com");    
        
        try {
            //设置浏览器窗口大小
            driver.manage().window().setSize(new Dimension(500, 500));

            //window.scrollTo(左边距, 右边距)
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            String jsContent = "window.scrollTo(10, 200)";
            jse.executeScript(jsContent);
        }
        catch(NoSuchElementException e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            driver.close();
        }    
    }
}

Python

from selenium import webdriver

driver = webdriver.Firefox()

# 隐式等待10S,打开网址
driver.implicitly_wait(10)
driver.get("http://www.baidu.com/")

try:
    # 设置浏览器窗体大小
    driver.set_window_size(500, 500)

    # window.scrollTo(左边距, 右边距)
    js = "window.scrollTo(10,200)"
    driver.execute_script(js)
except Exception as e:
    print(e.args[0])
finally:
    driver.close()

Ruby

class Baidu
  require 'rubygems'
  require 'selenium-webdriver'

  # 打开firefox并输入网址
  driver = Selenium::WebDriver.for :firefox

  # 设置隐式等待时间10S
  driver.manage.timeouts.implicit_wait = 10
  driver.navigate.to "http://www.baidu.com"

  begin
    # 设置浏览器窗体大小
    driver.manage.window.resize_to(500, 500)

    # window.scrollTo(左边距, 右边距)
    js = "window.scrollTo(10, 200)"
    driver.execute_script(js)
  rescue => e
    puts e.message # 显示报错信息
  ensure
    driver.close
  end
end

图片.gif

处理HTML5的视频播放

大多数浏览器使用控件(如Flash)来播放视频。但是,不同的浏览器需要使用不同的插件。HTML5定义了一个新元素video,指定了一个标准方式来嵌套视频。
JavaScript函数有个内置对象叫做arguments。argument对象包含了调用的参数数组,[0]表示取对象的第一个值
current 返回当前视频/音频的URL。如果未设置,则返回空字符串。
load()、play()、pause()等控制着视频的加载、播放和暂停。

Java

package JavaTest;

import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Test {
    public static void main(String[] arg) throws InterruptedException, IOException
    {
        WebDriver driver = new FirefoxDriver();

         // 设置隐示等待时长:10秒;
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.jq22.com/yanshi14795");    
        
        try {
            driver.switchTo().frame("iframe");
            WebElement video = driver.findElement(By.xpath("//*[@id='video1']"));
            
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            //返回播放文件地址
            String url = jse.executeScript("return arguments[0].currentSrc", video).toString();
            System.out.println(url);
            
            jse.executeScript("return arguments[0].play()", video); //播放视频
            Thread.sleep(5000);
            jse.executeScript("return arguments[0].pause()", video); //暂停视频
        }
        catch(NoSuchElementException e)
        {
            System.out.println(e.getMessage());
        }
        finally
        {
            driver.close();
        }    
    }
}

Python

from selenium import webdriver
from time import *

driver = webdriver.Firefox()

# 隐式等待10S,打开网址(可直接通过frame的id和name定位)
driver.implicitly_wait(10)
driver.get("http://www.jq22.com/yanshi14795")

try:
    driver.switch_to.frame("iframe")
    video = driver.find_element_by_xpath("//*[@id='video1']")

    # 返回播放文件地址
    url = driver.execute_script("return arguments[0].currentSrc;", video)
    print(url)

    driver.execute_script("return arguments[0].play()", video) # 播放视频
    sleep(5) # 播放5S
    driver.execute_script("return arguments[0].pause()", video) # 暂停视频
except Exception as e:
    print(e.args[0])
finally:
    driver.close()

Ruby

class Baidu
  require 'rubygems'
  require 'selenium-webdriver'

  # 打开firefox并输入网址
  driver = Selenium::WebDriver.for :firefox

  # 设置隐式等待时间10S
  driver.manage.timeouts.implicit_wait = 10
  driver.navigate.to "http://www.jq22.com/yanshi14795"

  begin
    driver.switch_to.frame('iframe')
    video = driver.find_element(:xpath => "//*[@id='video1']")

    # 返回播放文件地址
    url = driver.execute_script("return arguments[0].currentSrc;", video)
    puts url

    driver.execute_script("return arguments[0].play()", video) # 播放视频
    sleep(5)
    driver.execute_script("return arguments[0].pause()", video) # 暂停视频
  rescue => e
    puts e.message # 显示报错信息
  ensure
    driver.close
  end
end

图片.gif

posted on 2018-08-28 14:50  Q同码  阅读(465)  评论(0编辑  收藏  举报