对于使用Selenium处理浏览器身份验证感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解selenium操作浏览器,并且为您提供关于Javaselenium处理极验滑动验证码示例、java
对于使用Selenium处理浏览器身份验证感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解selenium操作浏览器,并且为您提供关于Java selenium处理极验滑动验证码示例、java – 使用selenium处理弹出窗口、java做一个模拟登陆,selenium被浏览器检测到了,滑动验证失败。不适用selenium就可以滑动验证,求大神帮助!、Selenium 常用 API 的使用 java 语言之 3-selenium3 浏览器驱动的宝贵知识。
本文目录一览:- 使用Selenium处理浏览器身份验证(selenium操作浏览器)
- Java selenium处理极验滑动验证码示例
- java – 使用selenium处理弹出窗口
- java做一个模拟登陆,selenium被浏览器检测到了,滑动验证失败。不适用selenium就可以滑动验证,求大神帮助!
- Selenium 常用 API 的使用 java 语言之 3-selenium3 浏览器驱动
使用Selenium处理浏览器身份验证(selenium操作浏览器)
有谁知道在自动化过程中使用Selenium或任何其他工具来处理浏览器身份验证吗?
答案1
小编典典警报方法authenticateUsing() 使您可以跳过“ Http基本身份验证”框。
WebDriverWait wait = new WebDriverWait(driver, 10); Alert alert = wait.until(ExpectedConditions.alertIsPresent()); alert.authenticateUsing(new UserAndPassword(username, password));
从Selenium 3.4开始,它仍处于测试阶段
现在,仅针对 InternetExplorerDriver
Java selenium处理极验滑动验证码示例
本篇文章主要介绍了Java selenium处理极验滑动验证码示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
要爬取一个网站遇到了极验的验证码,这周都在想着怎么破解这个,网上搜了好多知乎上看到有人问了这问题,我按照这思路去大概实现了一下。
1.使用htmlunit(这种方式我没成功,模拟鼠标拖拽后轨迹没生成,可以跳过)
我用的是java,我首先先想到了用直接用htmlunit,我做了点初始化
private void initWebClient() { if (webClient != null) { return; } webClient = new WebClient(browserVersion.FIREFOX_24); webClient.getoptions().setProxyConfig(new ProxyConfig("127.0.0.1",8888)); webClient.getoptions().setActiveXNative(true); webClient.getoptions().setUseInsecureSSL(true); // 配置证书 webClient.getoptions().setJavaScriptEnabled(true); webClient.getoptions().setCssEnabled(true); webClient.setCssErrorHandler(new SilentCssErrorHandler()); webClient.getoptions().setThrowExceptionOnScriptError(false); webClient.getoptions().setThrowExceptionOnFailingStatusCode(false); CookieManager cookieManager = new CookieManager(); List httpCookies = client.getCookies();//其方式获取的cookie for (org.apache.http.cookie.Cookie cookie : httpCookies) { cookieManager.addCookie(new com.gargoylesoftware.htmlunit.util.Cookie(cookie)); } webClient.setCookieManager(cookieManager); }
初始化代理,cookie..然后就能正常调用了
HtmlPage page = webClient.getPage("http://www.qixin.com/login");//企信宝 gePageInfor(page);
下面就是我获取图片,还原图片并且模拟拖拽,(这里我觉得是有些问题的,可能是拖拽我模拟的不对导致触发的js并没有生成正确的轨迹,还请大家帮忙看看哪里错了)
private void gePageInfor(HtmlPage page) { String[] img_slice={"div", "class", "gt_cut_fullbg_slice"}; String[] img_bg_slice={"div", "class", "gt_cut_bg_slice"}; HtmlDivision div = (HtmlDivision) page.getElementById("captcha"); int deCAPTCHA = 0; try { byte[] img_slice_binary = client.get(getimgurl(img_slice, div, true)).getBinary();//获取图片byte byte[] img_bg_slice_binary = client.get(getimgurl(img_bg_slice, div, false)).getBinary(); //获取还原后的图片 BufferedImage geetestImg = ImgTest.getGeetestImg(img_slice_binary, ImgTest.imgArray); BufferedImage geetestImg2 = ImgTest.getGeetestImg(img_bg_slice_binary, ImgTest.imgArray); //获得图片移动位置(目前还有问题,需改用第三方图片识别) deCAPTCHA =ImgTest.deCAPTCHA(geetestImg,geetestImg2); System.out.println(deCAPTCHA); } catch (IOException | FetchException e) { e.printstacktrace(); } HtmlDivision div_slider_knob = get_div_slider_knob(page,"gt_slider_knob gt_show");//获取要移动div HtmlPage mouSEOver = (HtmlPage) div_slider_knob.mouSEOver(); HtmlPage mouseDownPage = (HtmlPage)div_slider_knob.mouseDown(); div_slider_knob = get_div_slider_knob(mouseDownPage,"gt_slider_knob gt_show moving"); mouseMoveX(deCAPTCHA, div_slider_knob, mouseDownPage); HtmlPage newPage =(HtmlPage)div_slider_knob.mouSEOver(); // newPage =(HtmlPage)div_slider_knob.mouseDown(); System.out.println(newPage.asXml()); div = (HtmlDivision)newPage.getElementById("captcha"); HtmlElement htmlElement = div.getElementsByAttribute("div", "class", "gt_slice gt_show moving").get(0); System.out.println(htmlElement); newPage =(HtmlPage)div_slider_knob.mouseUp();//触发js,轨迹没有生成 System.out.println("---------------"); System.out.println(newPage.asXml()); if (newPage.getElementById("captcha")!=null) {//错误重试 //gePageInfor(newPage); } } private void mouseMoveX(int deCAPTCHA, HtmlDivision div_slider_knob, HtmlPage mouseDown) { MouseEvent mouseEvent = new MouseEvent(div_slider_knob, MouseEvent.TYPE_MOUSE_MOVE, false, false, false, MouseEvent.BUTTON_LEFT); mouseEvent.setClientX( mouseEvent.getClientX()+((deCAPTCHA!=0)?deCAPTCHA:99)); //移动x坐标 ScriptResult scriptResult = mouseDown.getDocumentElement().fireEvent(mouseEvent); } private HtmlDivision get_div_slider_knob(HtmlPage page,String classstring) { return (HtmlDivision)(((HtmlDivision) page.getElementById("captcha")).getElementsByAttribute("div", "class", classstring).get(0)); } private String getimgurl(String[] img_slice, HtmlDivision div, boolean isNeedCheckPostion) { String url =""; int[] postion = new int[2]; boolean empty = div.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]).isEmpty(); if (div.hasChildNodes() && !empty) { List elementsByAttribute = div.getElementsByAttribute(img_slice[0],img_slice[1],img_slice[2]); for(int i = 0;i
对比图片获取位移方法(deCAPTCHA)是错的我就不放代码了,下面是其中还原图片用的方法,目前是其实审查元素后你就明白怎么还原这个图片了,这里是每次读的10px,58px
public static BufferedImage getGeetestImg(byte[] binary, int[][] imgArray) throws IOException { BufferedImage img = ImageIO.read(new ByteArrayInputStream(binary)); List list = new ArrayList(); for (int i=0;i>>1; for (int i = 0; i
2.使用selenium
后来我想着是我模拟鼠标这个动作哪里有问题,我就又找到了selenium(2.42.2),他也能操作htmlunit关键他的鼠标动作好像封装比较完全
但是我尝试了以后发现了这个,HtmlUnitMouse这个动作没有实现
public void mouseMove(Coordinates where, long xOffset, long yOffset) { throw new UnsupportedOperationException("Moving to arbitrary X,Y coordinates not supported."); }
好吧,于是调用chrome吧
System.setProperty("webdriver.chrome.driver","C:\chromedriver.exe"); Proxy proxy = new Proxy(); //设置代理服务器地址 proxy.setHttpProxy("127.0.0.1:8888"); // DesiredCapabilities capabilities = DesiredCapabilities.htmlUnitWithJs(); DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(CapabilityType.PROXY, proxy); // final WebDriver driver = new HtmlUnitDriver(capabilities); WebDriver driver = new ChromeDriver(capabilities); driver.get("http://www.qixin.com/login"); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); checkPage(driver,"return $('.gt_cut_fullbg_slice');"); // 获取 网页的 title System.out.println("1 Page title is: " + driver.getTitle()); // 通过 id 找到 input 的 DOM String pageSource = driver.getPageSource(); System.out.println(pageSource); org.openqa.selenium.JavascriptExecutor executor = (org.openqa.selenium.JavascriptExecutor)driver; boolean equals = executor.executeScript("return document.readyState").equals("complete"); int moveX =99;//移动位置 if (equals) { WebElement element = driver.findElement(By.className("gt_slider_knob"));//(".gt_slider_knob")); Point location = element.getLocation(); element.getSize(); Actions action = new Actions(driver); // action.clickAndHold().perform();// 鼠标在当前位置点击后不释放 // action.clickAndHold(element).perform();// 鼠标在 onElement 元素的位置点击后不释放 // action.clickAndHold(element).moveByOffset(location.x+99,location.y).release().perform(); //选中source元素->拖放到(xOffset,yOffset)位置->释放左键 action.dragAndDropBy(element, location.x+moveX,location.y).perform(); // action.dragAndDrop(element,newelement).perform(); pageSource = driver.getPageSource(); } //更新cookie Set cookies = driver.manage().getCookies(); Set cookies2 = new HashSet(); for (org.openqa.selenium.Cookie cookie : cookies) { cookies2.add((Cookie) new Cookie(cookie.getDomain(), cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), true)); } for (Cookie cookie : cookies2) { org.apache.http.cookie.Cookie httpClient = cookie.toHttpClient(); } System.out.println(pageSource);
这样提交的表单确实是有轨迹的,这里移动位置我先写了个固定值,可以由上面图片还原,以及一些开源的图片识别工具识别出位置。以上应该就能解决这个滑动验证码了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持小编。
java – 使用selenium处理弹出窗口
请查看以下代码.
driver.findElement(By.linkText("Click me")).click();// when this line of code is reached then a popup window opens. System.out.println("After Clicking me"); // After the popup window opens this line of code is never executed.
我无法将控件从父窗口传输到弹出窗口.我知道以下命令.
driver.switchTo().window("popup window");
但它没有多大帮助.请帮我.
解决方法
它需要一个显式的webdriverwait,因为我在开发期间确实遇到了代码在窗口实际打开之前运行的问题,所以这可能不是一个理想的条件,
function manipulatePopUp(final WebDriver driver,final webdriverwait wait) { final String mainWindowHandle = driver.getwindowHandle(); driver.findElement(By.id("linkThatOpensPopUp")).click(); wait.until(new ExpectedConditions<Boolean>() { @Override public Boolean apply(WebDriver d) { return (d.getwindowHandles().size() != 1); } }); for (String activeHandle : driver.getwindowHandles()) { if (!activeHandle.equals(mainWindowHandle)) { driver.switchTo().window(activeHandle); } } driver.close(); driver.switchTo().window(mainWindowHandle); }
java做一个模拟登陆,selenium被浏览器检测到了,滑动验证失败。不适用selenium就可以滑动验证,求大神帮助!
Selenium 常用 API 的使用 java 语言之 3-selenium3 浏览器驱动
1. 下载浏览器驱动
当 selenium 升级到 3.0 之后,对不同的浏览器驱动进行了规范。如果想使用 selenium 驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动。 各浏览器下载地址: Firefox 浏览器驱动:geckodriver Chrome 浏览器驱动:chromedrivertaobao 备用地址 IE 浏览器驱动:IEDriverServer Edge 浏览器驱动:MicrosoftWebDriver Opera 浏览器驱动:operadriver PhantomJS 浏览器驱动:phantomjs
2. 设置浏览器驱动
设置浏览器的地址非常简单。 我们可以手动创建一个存放浏览器驱动的目录,如: C:\driver , 将下载的浏览器驱动文件(例如:chromedriver、geckodriver)丢到该目录下。 我的电脑–> 属性–> 系统设置–> 高级–> 环境变量–> 系统变量–>Path,将 “C:\driver” 目录添加到 Path 的值中。
3. 验证浏览器驱动
验证不同的浏览器驱动是否正常使用。
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
……
WebDriver driver = new ChromeDriver(); //Chrome浏览器
WebDriver driver = new FirefoxDriver(); //Firefox浏览器
WebDriver driver = new EdgeDriver(); //Edge浏览器
WebDriver driver = new InternetExplorerDriver(); // Internet Explorer浏览器
WebDriver driver = new OperaDriver(); //Opera浏览器
WebDriver driver = new PhantomJSDriver(); //PhantomJS
……
今天关于使用Selenium处理浏览器身份验证和selenium操作浏览器的讲解已经结束,谢谢您的阅读,如果想了解更多关于Java selenium处理极验滑动验证码示例、java – 使用selenium处理弹出窗口、java做一个模拟登陆,selenium被浏览器检测到了,滑动验证失败。不适用selenium就可以滑动验证,求大神帮助!、Selenium 常用 API 的使用 java 语言之 3-selenium3 浏览器驱动的相关知识,请在本站搜索。
本文标签: