本文将介绍在OSX中使用SeleniumWebDriver打开和关闭新选项卡的详细情况,特别是关于selenium关闭新打开窗口的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这
本文将介绍在OS X中使用Selenium WebDriver打开和关闭新选项卡的详细情况,特别是关于selenium 关闭新打开窗口的相关信息。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?、Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测、Selenium WebDriver:打开新选项卡,而不是新窗口、selenium-webdriver – 如何在Selenium WebDriver中单击网页中的所有链接的知识。
本文目录一览:- 在OS X中使用Selenium WebDriver打开和关闭新选项卡(selenium 关闭新打开窗口)
- Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?
- Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测
- Selenium WebDriver:打开新选项卡,而不是新窗口
- selenium-webdriver – 如何在Selenium WebDriver中单击网页中的所有链接
在OS X中使用Selenium WebDriver打开和关闭新选项卡(selenium 关闭新打开窗口)
我正在Windows的Python 2.7中使用Firefox Webdriver来模拟打开(Ctrl
+ t
)和关闭(Ctrl
+w
)新标签页。
这是我的代码:
from selenium import webdriver from selenium.webdriver.common.keys import Keysbrowser = webdriver.Firefox()browser.get(''https://www.google.com'')main_window = browser.current_window_handle# open new tabbrowser.find_element_by_tag_name(''body'').send_keys(Keys.CONTROL + ''t'')browser.get(''https://www.yahoo.com'')# close tabbrowser.find_element_by_tag_name(''body'').send_keys(Keys.CONTROL + ''w'')
如何在Mac上实现相同功能?基于此评论,应该使用它browser.find_element_by_tag_name(''body'').send_keys(Keys.COMMAND+ ''t'')
来打开一个新选项卡,但是我没有Mac可以对其进行测试,等效于Ctrl-w
什么?
谢谢!
答案1
小编典典没有什么比运行JavaScript更容易,更清晰了。
开启新分页: driver.execute_script("window.open('''');")
Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?
首先我们来看一个经典的例子: 搭出租车
在出租车驾驶中,通常有3个角色:
- 乘客 : 他告诉出租车司机他想去哪里以及如何到达那里
对出租车司机说:
1、去阳光棕榈园东门
2、从这里转左
3、然后直行 200米,第一个红绿灯右转
4、再直行 50米,到阳光棕榈园东门
5、停车
- 出租车司机 : 他按照客户的要求; 出租车司机使用方向盘和汽车踏板驾驶汽车送乘客去目的地
出租车司机操作汽车:
1、插钥匙点火,启动汽车引擎
2、开一小段路后左转
3、加速,直行200米
4、右转,直行50米
5、减速停车,到达阳光棕榈园东门
- 汽车 : 汽车执行出租车司机的操作
1、启动引擎
2、左转
3、直行
4、右转
5、停车
在使用Selenium WebDriver的测试自动化中,有3个角色
- 编写自动化代码的测试工程师 : 运行自动化代码将请求发送到浏览器驱动
告诉浏览器 :
1、打开 www.abc.com
2、做一个关键字搜索 selenium
3、检查实际结果,与预期结果做比较
- 浏览器驱动 : 它执行测试工程师的请求,然后它向浏览器发送请求
告诉浏览器 :
1、给我打开这个页面 www.abc.com
2、当textbox显示可见,保存这个web element 3、操作textbox这个对象,输入selenium 4、当确认按钮可以点击,保存这个web element对象 5、点击这个按钮对象
- 浏览器 : 它执行浏览器驱动的请求
1、打开www.abc.com
2、找到搜索框textbox,输入selenium 3、点击搜索按钮 4、展示搜索结果
做一个类比:
- 测试工程师就像客户
- 浏览器驱动就像一个出租车司机
- 浏览器就像一辆车租车
执行自动化脚本,会执行以下步骤:
- 对于每个Selenium命令,都会创建一个HTTP请求并将其发送到浏览器驱动程序
- 浏览器驱动使用HTTP服务器来获取HTTP请求
- HTTP服务器确定实现Selenium命令所需的步骤
- 实现步骤在浏览器上执行
- 执行状态被发送回HTTP服务器
- 在HTTP服务器返回该状态的自动化脚本
Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测
我正在尝试使用selenium和铬在网站中自动化一个非常基本的任务,但是以某种方式网站会检测到铬是由selenium驱动的,并阻止每个请求。我怀疑该网站是否依赖像这样的公开DOM变量来检测selenium驱动的浏览器。
我的问题是,有没有办法使navigator.webdriver标志为假?我愿意尝试修改后重新尝试编译selenium源,但似乎无法在存储库中的任何地方找到NavigatorAutomationInformation源https://github.com/SeleniumHQ/selenium
任何帮助深表感谢
PS:我还从https://w3c.github.io/webdriver/#interface尝试了以下操作
Object.defineProperty(navigator, ''webdriver'', { get: () => false, });
但是它仅在初始页面加载后更新属性。我认为网站会在执行脚本之前检测到变量。
答案1
小编典典首先更新1
execute_cdp_cmd()
:随着execute_cdp_cmd(cmd,cmd_args)
命令的可用性,现在您可以使用Selenium轻松执行google-chrome-
devtools 命令。使用此功能,您可以轻松修改,以防止检测到Seleniumnavigator.webdriver
防止检测 2
为了防止检测到Selenium驱动的 WebDriver ,利基方法将包括以下所有步骤之一或全部:
- 通过命令旋转用户代理
execute_cdp_cmd()
,如下所示:
#Setting up Chrome/83.0.4103.53 as useragent driver.execute_cdp_cmd(''Network.setUserAgentOverride'', {"userAgent": ''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36''})
- 将for webdriver* 的 属性 值更改为 undefined
navigator
***
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, ''webdriver'', { get: () => undefined }) """ })
排除
enable-automation
开关的集合options.add_experimental_option("excludeSwitches", ["enable-automation"])
关掉
useAutomationExtension
options.add_experimental_option(''useAutomationExtension'', False)
示例代码3
汇总上述所有步骤和有效的代码块,将是:
from selenium import webdriver options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option(''useAutomationExtension'', False) driver = webdriver.Chrome(options=options, executable_path=r''C:\WebDrivers\chromedriver.exe'') driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """ Object.defineProperty(navigator, ''webdriver'', { get: () => undefined }) """ }) driver.execute_cdp_cmd(''Network.setUserAgentOverride'', {"userAgent": ''Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36''}) print(driver.execute_script("return navigator.userAgent;")) driver.get(''https://www.httpbin.org/headers'')
历史
根据 W3C编辑器草案 ,当前的实现严格提到:
当 用户代理 受 远程控制 时,该 标志 设置为,最初设置为。
webdriver-active
true
__false
进一步,
Navigator includes NavigatorAutomationInformation;
要注意的是:
该
NavigatorAutomationInformation
接口 不应在 WorkerNavigator 上 公开 。
该NavigatorAutomationInformation
接口 定义为:
interface mixin NavigatorAutomationInformation { readonly attribute boolean webdriver;};
true
如果设置了webdriver-active
标志, 则返回,否则返回false。
最后,navigator.webdriver
定义了一种标准方法,用于使用户代理合作以通知文档它由 WebDriver
控制,以便可以在自动化过程中触发备用代码路径。
警告 :更改/调整上述参数可能会阻止 导航 并获取检测到的 WebDriver 实例。
更新(2019年11月6日)
从当前的实现开始,一种理想的访问网页而不被检测到的理想方法是使用ChromeOptions()
该类向以下参数添加几个参数:
- 排除
enable-automation
开关的集合 - 关掉
useAutomationExtension
通过以下实例ChromeOptions
:
- Java示例:
System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe"); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation")); options.setExperimentalOption("useAutomationExtension", false); WebDriver driver = new ChromeDriver(options); driver.get("https://www.google.com/");
- Python范例
from selenium import webdriver options = webdriver.ChromeOptions() options.add_experimental_option("excludeSwitches", ["enable-automation"]) options.add_experimental_option(''useAutomationExtension'', False) driver = webdriver.Chrome(options=options, executable_path=r''C:\path\to\chromedriver.exe'') driver.get("https://www.google.com/")
传说
1:仅适用于Selenium的Python客户端。
2:仅适用于Selenium的Python客户端。
3:仅适用于Selenium的Python客户端。
Selenium WebDriver:打开新选项卡,而不是新窗口
如何解决Selenium WebDriver:打开新选项卡,而不是新窗口?
硒目前无法切换标签。因此,我们迫使浏览器在新窗口中打开链接,但是由于我们能够切换窗口,因此我们迫使浏览器采用这种方法。这 会在更高版本中修复
解决方法
我正在使用Selenium WebDriver。每个链接都会在新的浏览器窗口中打开。这对我来说不方便。如何更改它以便仅在新选项卡中打开?
selenium-webdriver – 如何在Selenium WebDriver中单击网页中的所有链接
条件是:
i)我不知道有多少链接
ii)我想计算并点击每个链接
请建议我使用Selenium WebDriver脚本.
解决方法
迭代器和高级for循环可以做类似的工作;但是,循环内页面导航的不一致性可以使用数组概念来解决.
private static String[] links = null; private static int linksCount = 0; driver.get("www.xyz.com"); List<WebElement> linksize = driver.findElements(By.tagName("a")); linksCount = linksize.size(); System.out.println("Total no of links Available: "+linksCount); links= new String[linksCount]; System.out.println("List of links Available: "); // print all the links from webpage for(int i=0;i<linksCount;i++) { links[i] = linksize.get(i).getAttribute("href"); System.out.println(all_links_webpage.get(i).getAttribute("href")); } // navigate to each Link on the webpage for(int i=0;i<linksCount;i++) { driver.navigate().to(links[i]); Thread.sleep(3000); }
1 |捕获特定帧| class | id下的所有链接并逐个导航
driver.get("www.xyz.com"); WebElement element = driver.findElement(By.id(Value)); List<WebElement> elements = element.findElements(By.tagName("a")); int sizeOfAllLinks = elements.size(); System.out.println(sizeOfAllLinks); for(int i=0; i<sizeOfAllLinks ;i++) { System.out.println(elements.get(i).getAttribute("href")); } for (int index=0; index<sizeOfAllLinks; index++ ) { getElementWithIndex(By.tagName("a"),index).click(); driver.navigate().back(); } public WebElement getElementWithIndex(By by,int index) { WebElement element = driver.findElement(By.id(Value)); List<WebElement> elements = element.findElements(By.tagName("a")); return elements.get(index); }
2 |捕获所有链接[替代方法]
Java的
driver.get(baseUrl + "https://www.google.co.in"); List<WebElement> all_links_webpage = driver.findElements(By.tagName("a")); System.out.println("Total no of links Available: " + all_links_webpage.size()); int k = all_links_webpage.size(); System.out.println("List of links Available: "); for(int i=0;i<k;i++) { if(all_links_webpage.get(i).getAttribute("href").contains("google")) { String link = all_links_webpage.get(i).getAttribute("href"); System.out.println(link); } }
Python
from selenium import webdriver driver = webdriver.Firefox() driver.get("https://www.google.co.in/") list_links = driver.find_elements_by_tag_name('a') for i in list_links: print i.get_attribute('href') driver.quit()
关于在OS X中使用Selenium WebDriver打开和关闭新选项卡和selenium 关闭新打开窗口的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Selenium WebDriver原理(一):Selenium WebDriver 是怎么工作的?、Selenium Webdriver:修改navigator.webdriver标志以防止selenium检测、Selenium WebDriver:打开新选项卡,而不是新窗口、selenium-webdriver – 如何在Selenium WebDriver中单击网页中的所有链接的相关信息,请在本站寻找。
本文标签: