这里直接给代码例子,大家自己体会,不过需要注意的是,以下代码运行时的Firefox版本是60.0.1,OS是Win 10,如果浏览器版本不一样,OS不一样,具体界面可能也会不一样,这个时候就需要大家用SuperPAtrick定位器自己微调代码:


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class WebDriverDemo {
   public interface SuperPatrickLibrary extends Library {
       void findElement(String pStrId, String pStrName, String pStrClassName, String controlType);
       void sendKeys(String pKeysString);
       void sendShortCutKeys(String pKeysString);
   }

   public static void main(String[] args) throws InterruptedException {
       System.setProperty("jna.encoding","GBK");
       // TODO Auto-generated method stub
       
String dllPath = "C:/1/SuperPatrickLibrary.dll";
       SuperPatrickLibrary superpatrick = (SuperPatrickLibrary) Native.loadLibrary(dllPath, SuperPatrickLibrary.class);
       superpatrick.findElement("307","显示桌面","","Button");
       WebDriver driver = new FirefoxDriver();

       String url ="http://www.autotestops.com/test/upload.html";
       driver.get(url);
       Thread.sleep(5000);
       superpatrick.findElement("","Firefox","","Button");
       superpatrick.findElement("","定制…","","Button");
       Thread.sleep(500);
       superpatrick.findElement("","工具栏","","Button");
       Thread.sleep(500);
       superpatrick.findElement("","菜单栏","","MenuItem");
       Thread.sleep(500);
       superpatrick.findElement("","完成","","Button");

     
Thread.sleep(500);
       superpatrick.findElement("","历史","","MenuItem");
       superpatrick.findElement("","清除最近的历史记录…    Ctrl+Shift+Del","","MenuItem");
       Thread.sleep(1000);
       superpatrick.findElement("","要清除的时间范围:","","ComboBox");
       superpatrick.findElement("","最近一小时","","ListItem");
       superpatrick.findElement("","详细信息","","Button");
       Thread.sleep(500);
       superpatrick.findElement("","浏览和下载历史","","Text");
       superpatrick.findElement("","表单和搜索历史","","Text");
       superpatrick.findElement("","登录状态","","Text");
       Thread.sleep(500);
       System.out.println("设置完成");
       superpatrick.findElement("","立即清除","","Button");
       Thread.sleep(5000);

     
superpatrick.findElement("","浏览…","","Button");
       Thread.sleep(500);
       superpatrick.findElement("1001","地址: 桌面","ToolbarWindow32","ToolBar");
       String path = System.getProperty("user.dir");
       superpatrick.sendKeys(path);
       superpatrick.sendShortCutKeys("{Return}");
       superpatrick.findElement("1148","文件名(N):","ComboBox","ComboBox");
       superpatrick.sendKeys("1.jpg");
       Thread.sleep(500);
       superpatrick.findElement("1","打开(O)","Button","Button");
     
driver.quit();
   }

}


从以上示例代码可以看出,SuperPtrick和webdriver完全互补,webdriver超出浏览器范围的部分将无能为力,而SuperPatrick可以弥补这个欠缺,同时在定位浏览器页面元素的时候,webdriver搞不定的时候,不妨试试SuperPatrick,  常常可以帮你搞定webdriver很难处理的问题,而且SuperPatrick不会改变webdriver内部任何状态,webdriver可以继续处理剩下的问题。

super(3).gif

SuperXi