在selenium中window.open()和click()点击链接的区别
2018-01-12 10:06
405 查看
在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"这个属性,然后再去排查代码的错误。
分为两种情况:
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"这个属性,然后再去排查代码的错误。
相关文章推荐
- 捕获浏览器的前进、后退事件 window.onhashchange 并区别于点击链接
- 用window.location和window.open做链接的区别
- window.location和window.open做链接的区别
- 用window.location和window.open做链接的区别
- 用window.location和window.open做链接的区别
- 用window.location和window.open做链接的区别
- window.open的参数说明+点击链接窗…
- 用window.location和window.open做链接的区别
- 用window.location和window.open做链接的区别
- 从A页面单击 window.open 打开了B页面 B页面没有关闭再次点击A页面的这个链接, 让他刷新B页面而不是在打开一个B页面可以做到吗?
- Gridview中点击链接window.open一个新窗口(showModalDialog()、showModelessDialog()方法使用详解)
- (转载)HTML:模拟链接被按下,在新标签页打开页面,不使用window.open(可能被拦截)
- window.open、window.showModalDialog和window.showModelessDialog 的区别
- JS点击任意标签获得该标签属性,以获得ID为例,以及AJAX的异步原理和 $(document).ready()与window.onload加载方法的区别
- trigger伪造a标签点击代替window.open
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
- javascript 打开页面window.location和window.open的区别
- Andriod动画中windowEnterAnimation和activityOpenEnterAnimation的区别
- js 中实现页面跳转的方法(window.location和window.open的区别)
- window.location和window.open的区别