use*_*083 2 java arrays selenium webdriver selenium-webdriver
我添加了网页中的所有链接Arraylist,然后一一点击所有网址。
public class Redirectionlinked1
{
public static List findAllLinks(WebDriver driver)
{
List <WebElement> elementList = new ArrayList();
elementList = driver.findElements(By.tagName("a"));
elementList.addAll(driver.findElements(By.tagName("img")));
List finalList = new ArrayList();
for(WebElement element : elementList)
{
if (element.getAttribute("href") != null)
{
finalList.add(element);
}
}
return finalList;
}
public static void main(String[] args) throws Exception
{
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\gecko\\geckodriver-v0.16.1-win64\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(" http://testsite.com");
List <WebElement > allImages = findAllLinks(driver);
System.out.println("Total number of elements found " + allImages.size());
driver = new ChromeDriver ();
URI uri =null;
for (WebElement element : allImages) {
if (!driver.getCurrentUrl().equals(element.getAttribute("href")) && driver.)
{
driver.manage().deleteAllCookies();
driver.get(element.getAttribute("href"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(500);
System.out.println(element.getAttribute("href"));
uri = new URI(driver.getCurrentUrl());
try
{
if(uri.getHost().equalsIgnoreCase("SpecificDomain.net"))
{
System.out.println(" Redirected URL-->> "+element.getAttribute("href"));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
代码按预期工作(它在浏览器中启动 URL),第一个链接稍后会抛出错误:
线程“main”org.openqa.selenium.InvalidArgumentException中出现异常:未知错误:不支持的协议(会话信息:chrome=58.0.3029.110)(驱动程序信息:chromedriver=2.26.436362(5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP 1 x86_64)(警告:服务器未提供任何堆栈跟踪信息)命令持续时间或超时:131 毫秒构建信息:版本:'未知',修订版:'3169782',时间:'2016-09-29 10:24:50 - 0700'系统信息:主机:'ETPUN-LT009',ip:'192.168.2.193',os.name:'Windows 7',os.arch:'amd64',os.version:'6.1',java.version: “1.8.0_111”驱动程序信息:org.openqa.selenium.chrome.ChromeDriver 功能 [{applicationCacheEnabled=false、rotatable=false、mobileEmulationEnabled=false、networkConnectionEnabled=false、chrome={chromedriverVersion=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30) , 用户数据目录= C:\scoped_dir12784_32532},takeHeapSnapshot = true,pageLoadStrategy =正常,databaseEnabled = false,handlesAlerts = true,hasTouchScreen = false,version = 58.0.3029.110,platform = XP,browserConnectionEnabled = false,nativeEvents = true,acceptSslCerts = true,locationContextEnabled = true,webStorageEnabled = true,browserName = chrome,takeScreenshot = true,javascriptEnabled = true,cssSelectorsEnabled = true,unexpectedAlertBehaviour =}]会话ID:df813868289a8f15f947ac620b3b1882在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)在sun.reflect.NativeConstructorAccessor实现newInstance (NativeConstructorAccessorImpl.java:62) 在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.lang.reflect.Constructor.newInstance(Constructor.java:423) 在 org.openqa.selenium.remote.ErrorHandler。 createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:164) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:636) 在 org.openqa .selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:323) 在 Practices.Redirectionlinked1.main(Redirectionlinked1.java:99)
我的配置是:-
Chrome - 版本 58.0.3029.110(64 位)
Geckodriver-v0.16.1-win64
Windows 7的
Java - 1.8.1
这可能是因为您的网站中有一些类似于 hre 的链接#,resources/123.img这些链接不是完整的 URL,触发 get 会导致异常。您应该检查以确保网址有效。这可以通过使用比较来降低link.startsWith("http://") || link.startsWith("https://")
还有其他地方你的测试也会失败。
finalList被声明为列表并返回。必须将其更改为列表,并应使用链接值进行填充。这是因为我们有一个 for 循环,您在其中调用 driver.get(newLink) ,这将重置中的所有 WebElement 对象,因为finalList它们是较早发现的并给出异常。
img标签没有href. 而是使用“src”。
这是所有这些更改之后的代码。请注意,可能还有其他条件来检查 URL 是否有效,我在此未列出。
public static List<String> findAllLinks(WebDriver driver) {
// Declare finalList as string.
List<String> finalList = new ArrayList<>();
// Get the a tags
List<WebElement> elementList = driver.findElements(By.tagName("a"));
// get the img tags
elementList.addAll(driver.findElements(By.tagName("img")));
for (WebElement element : elementList) {
// a tags have "href", img tags have src
String link = element.getTagName().equalsIgnoreCase("a") ? element.getAttribute("href")
: element.getAttribute("src");
// Check if link is not null and whether is a valid link by checking
// starts with http or https
if (link != null && (link.startsWith("http://") || link.startsWith("https://"))) {
finalList.add(link);
}
}
return finalList;
}
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.gecko.driver",
"E:\\Softwares\\gecko\\geckodriver-v0.16.1-win64\\geckodriver.exe");
System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://testsite.com");
List<String> allLinks = findAllLinks(driver);
System.out.println("Total number of elements found " + allLinks.size());
driver = new ChromeDriver();
URI uri = null;
for (String link : allLinks) {
if (!driver.getCurrentUrl().equals(link)) {
driver.manage().deleteAllCookies();
driver.get(link);
Thread.sleep(500);
System.out.println(link);
uri = new URI(driver.getCurrentUrl());
try {
if (uri.getHost().equalsIgnoreCase("SpecificDomain.net")) {
System.out.println("Redirected URL-->> " + link);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)