在selenium中window.open()和click()点击链接的区别

在selenium中点击一个链接,打开一个新的页面常用的有两种方法:click和window.open()。

      分为两种情况:

     1.链接没有target="_blank"属性

网页示例代码:

<html>
<head>
    <title>打开窗口</title>
</head>
<body>
    <div id="target">
		<ul class="hearder_ul">
			<li class="nv_list1">
				<a href="http://www.baidu.com/" class="nav_item" >首页001</a>
			</li>
		</ul>
	</div>
</body>
</html>
     当使用click点击“首页001”时的效果就是,在浏览器的当前tab直接打开“ http://www.baidu.com/ ”页面。

package com.keydom;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenWindow {

	public static WebDriver driver;
	public static void main(String[] args) throws Exception {

		//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		
		driver.get("file:///C:/Users/Uker/Desktop/seleniumHTML/openwindow.html");
		Thread.sleep(1000);
		
		WebElement element = driver.findElement(By.xpath("//*[@id=\"target\"]/ul/li[1]/a"));  //定位目标元素
		element.click();  //click方法点击目标元素
		Thread.sleep(1000);
		
		driver.quit();
		
	}

}
   当使用window.open()点击“首页001”是的效果就是,浏览会new一个tab来打开 "http://www.baidu.com/"页面


package com.keydom;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenWindow {

	public static WebDriver driver;
	public static void main(String[] args) throws Exception {

		//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		
		driver.get("file:///C:/Users/Uker/Desktop/seleniumHTML/openwindow.html");
		Thread.sleep(1000);
		
		WebElement element = driver.findElement(By.xpath("//*[@id=\"target\"]/ul/li[1]/a"));  //定位目标元素
		String url = element.getAttribute("href");  //获取目标元素的url
		((JavascriptExecutor)driver).executeScript("window.open('"+url+"')");  //window.open()点击目标元素
		Thread.sleep(5000);
		
		driver.quit();
		
	}

}

2.链接有 target="_blank"

<html>
<head>
    <title>打开窗口</title>
</head>
<body>
    <div id="target">
		<ul class="hearder_ul">
			<li class="nv_list2">
				<a href="http://www.baidu.com/" class="nav_item" target="_blank">首页002</a>
			</li>
		</ul>
	</div>
</body>
</html>

当“首页002”这个超链接有target="_blank"这个属性的时候,用click和window.open()点击“首页002”的时候效果都是一样的:浏览会new一个tab来打开"http://www.baidu.com/"页面。

package com.keydom;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class OpenWindow {

	public static WebDriver driver;
	public static void main(String[] args) throws Exception {

		//设置chromedriver驱动位置,因为chromedriver没有在eclipse目录下
		System.setProperty("webdriver.chrome.driver", "E:\\eclipse_jar\\selenium_jar\\chromedriver.exe");
		driver = new ChromeDriver();
		
		driver.get("file:///C:/Users/Uker/Desktop/seleniumHTML/openwindow.html");
		Thread.sleep(1000);
		
		WebElement element1 = driver.findElement(By.xpath("//*[@id=\"target\"]/ul/li[2]/a"));
		/**
		 * 当超链接有target="_blank"时,click()和window.open()的效果是一样的
		 */
//		element1.click(); 
		String url1 = element1.getAttribute("href");
		((JavascriptExecutor)driver).executeScript("window.open('"+url1+"')");
		Thread.sleep(5000);
		
		driver.quit();
		
	}

}

所以,当发现click和window.open()点击后的效果是一样的时候,首先要去检查一下超链接是否有target="_blank"这个属性,然后再去排查代码的错误。

猜你喜欢

转载自blog.csdn.net/lykio_881210/article/details/79040860
今日推荐