当一个网页上已经打开了一个或多个窗口时,我们可以使用selenium找出已经打开的窗口数。但是,有没有任何方法可以通过任何标签或任何其他方式来确定使用selenium可以在给定的网页上实际打开多少个窗口呢。
例如,我们有一个锚定标签为所有的网址,这是目前在一个网页上,所以有没有办法找出多少窗口可以打开一个网页或点击多少按钮/链接,一个窗口将被打开。
在任何java或python和任何网页的解决方案将不胜感激。
当一个网页上已经打开了一个或多个窗口时,我们可以使用selenium找出已经打开的窗口数。但是,有没有任何方法可以通过任何标签或任何其他方式来确定使用selenium可以在给定的网页上实际打开多少个窗口呢。
例如,我们有一个锚定标签为所有的网址,这是目前在一个网页上,所以有没有办法找出多少窗口可以打开一个网页或点击多少按钮/链接,一个窗口将被打开。
在任何java或python和任何网页的解决方案将不胜感激。
3条答案
按热度按时间oo7oh9g91#
直截了当地回答,事实上没有确切的方法来计算使用selenium可以从一个网页打开多少(子)窗口。
:锚定元件
html
<a>
元素(或锚元素)定义一个超链接,用于从一个页面链接到其他网页、文件、同一页面中的位置、电子邮件地址或任何其他url。最重要的属性<a>
元素是href属性,它指示链接的目标。目标属性只能与锚定标记中的href属性一起使用,方法如下:如果没有使用target属性,那么链接将在同一页中打开。
举个例子:
如果目标属性设置为
_blank
,链接将在新的浏览器窗口或新的选项卡中打开。举个例子:
现在你可以通过html标签触发新标签/窗口的打开,除非测试环境有足够的内存,共享内存,等等。您可以在未知错误中找到相关的讨论:会话已删除,因为未知错误导致页面崩溃:无法确定从chromedriver selenium崩溃的选项卡加载状态
注意:一个重要的方面是,如果您正在打开一个新的选项卡/窗口,并打算将selenium的焦点切换到新打开的选项卡/窗口,则需要按如下方式诱导webdriverwait:
(java示例)expectedconditions为
numberOfWindowsToBe(int expectedNumberOfWindows)
:在这里你可以找到一个详细的讨论
(python示例)预期的\u条件为
number_of_windows_to_be(num_windows)
:在这里你可以找到一个详细的讨论
wljmcqd82#
是的,根据锚定标记,您可以找到可以从页面打开的链接数,并且可以在“n”窗口中打开链接,因此在windows中打开链接后,您可以获得打开的窗口计数,请尝试以下代码:
上面的代码将计算有多少'href'存在,但不能告诉有多少窗口可以打开,因为您可以打开'n'个窗口。通过使用以下代码,您可以找到打开的窗口计数:
ukdjmx9f3#
我得到你的问题基本上你想看看有多少窗口可以打开一个网页。
为了做到这一点,你可以使用下面的xpath,这将给你的网页将在当前网页上打开计数
您可以使用如下代码: