注意:一定要搞清楚,你的系统是win64还是32的,然后下载相应版本的selenium-server-standalone-3.3.x.jar和selenium-java-3.3.x.zip,要不然会报各种错误,你还不知道该怎么解决的。。。。 [蜜汁微笑]
点击这里下载各种版本的jar 把相应的driverServer.exe放入浏览器的安装目录下,比如:IEDriverServer.exe放入本地磁盘C:\Program Files\Internet Explorer目录下
基础定位方法:
方法举例说明By.idWebElement element = driver.findElement(By.id(“appId”));通过元素ID定位By.nameWebElement element = driver.findElement(By.name(“app”));通过元素name定位By.classNameList< WebElement> elements = driver.findElements(By.className(“app”));通过class定位By.tagNameList< WebElement> elements = driver.findElements(By.tagName(“input”));通过标签名称定位By.linkTextWebElement element = driver.findElement(By.linkText(“百度”));通过链接的文字定位。例如:`< a href=“www.baidu.com”>百度</ a>By.partialLinkTextWebElement element = driver.findElement(By.partialLinkText(“百度”));通过链接字模糊定位。如果一个有文字链接的元素,上面文字太多,不想写这么多文字,那么就可以用PartialLinkText,也就是用LinkText里面的一部分字符就可以定位该元素。选取的字符要有唯一性,也就是根据你选取的字符,在当前页只能找到你的目标元素。重要定位方法: By.xpath
xpath定位方法举例利用元素属性定位WebElement element = driver.findElement(By.xpath("//input[@οnclick=‘Open_tab(2)’]"));绝对路径定位WebElement wElement = driver.findElement(By.xpath("/html/body/div/form/input"))相对路径WebElement wElement = driver.findElement(By.xpath("//form/span/input"))级与属性结合WebElement wElement = driver.findElement(By.xpath("//span[@class=‘bg s_ipt_wr iptfocus quickdelete-wrap’]/input"))使用逻辑运算符WebElement wElement = driver.findElement(By.xpath("//input[@class=‘s_ipt’ and @id=‘kw’]"))模糊查找driver.findElement(By.xpath("//input[contains(@onclick,‘TTmoasAppTmxzAppform’)]")).sendKeys(Keys.ENTER)By.cssSelector 根据tagName:driver.findElement(By.cssSelector(“input”) 根据ID:driver.findElement(By.cssSelector(“input#username”));//html标签和id 参考:cssSelector之selenium元素定位
((JavascriptExecutor)driver).executeScript("isOpen(2)");
如果在页面找不到id为code元素,显式等待code的元素最多10秒时间,如果10秒后还没获取到code元素则抛出异常
WebDriverWait wait = new WebDriverWait(driver, 10);//等待元素10秒 wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d){ boolean loadcomplete=driver.findElement(By.id("code")).isDisplayed(); return loadcomplete; } });下拉列表的id为appdq:
Select select = new Select(driver.findElement(By.id("appdq"))) select.selectByVisibleText("A");//选择A select.selectByValue("1"); select.deselectAll(); select.deselectByValue("1"); select.deselectByVisibleText("A"); select.getAllSelectedOptions(); select.getFirstSelectedOption();建议使用driver.quit()。
可以尝试执行JavaScript代码的方式解决, 代码1:
driver.findElement(By.id("share1")).click();改为:
((JavascriptExecutor)driver).executeScript("document.getElementById('share1').click();");代码2:
driver.findElement(By.id("sex")).sendKeys((user.getSex()).toUpperCase());改为:
String sex="document.getElementById('sex').value='"+user.getSex()+"';"; JavascriptExecutor js = (JavascriptExecutor)driver; js.executeScript(sex);类似这种,设置元素为readOnly 通过执行js代码,删除readOnly属性
((JavascriptExecutor)driver).executeScript("document.getElementById('apporregNum').removeAttribute(\"readOnly\");");有时程序执行太快,网页还没加载完毕,这时需要在代码中让线程休息几秒钟Thread.sleep(2000);,有好多次出错都是因为执行太快了。
1.对异常的处理,对每个元素执行一次操作就进行异常捕获,防止抛出异常导致程序无法继续执行; 2.如果要获取页面上的数据,可以使用jsoup,或者是selenium+jsoup;
driver.get(INDEX_URL);//打开浏览器,访问网址 String pageSource = driver.getPageSource(); Document doc = Jsoup.parse(pageSource); doc.select("#mainsrp-itemlist .item");//#对应的id,.对应class3.httpunit 4.执行js代码时对单引号或双引号的需要转义
String nameCn = "I'm a girl".replaceAll("'", "\\\\'");//将单引号转义为\' String appCnName="document.getElementById('appCnName').value='"+nameCn+"';"; js.executeScript(appCnName);