I want to scan all opened browser windows for input tags and submit buttons. I have opened all these windows by grabbing links from loaded page. My code is following.
我想扫描所有打开的浏览器窗口以获取输入标签和提交按钮。我通过从加载的页面抓取链接打开了所有这些窗口。我的代码如下。
require 'rubygems'
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
page = driver.get " http://testasp.vulnweb.com "
link = Array.new(driver.find_elements(:tag_name, "a"))
link.each do |a|
a = driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", a)
a.click
end
i = driver.window_handles
I am able to get all opened window IDs. How I can find input elements from all these opened browser windows and also submit buttons and forms? window_handles
does not accept any arguments when I tried that.
我能够获得所有打开的窗口ID。如何从所有这些打开的浏览器窗口中找到输入元素并提交按钮和表单?我试过的时候,window_handles不接受任何参数。
2 个解决方案
#1
1
To switch to the popups, you would use:
要切换到弹出窗口,您可以使用:
driver.switch_to().window(handle)
So could do something like this:
所以可以做这样的事情:
#Get all of the window handles
i = driver.window_handles
#Iterate through the popups
#Note that starting at index 1 assuming we do not care about the original window
i[1..i.length].each do |handle|
#Switch to popup
driver.switch_to().window(handle)
#Do whatever processing you want on the popup
#Example, get the number of input fields
inputs = driver.find_elements(:tag_name, 'input')
puts inputs.length
#Probably want to close the popup
driver.close
end
#Switch back to the original window
driver.switch_to().window(i[0])
#Do whatever processing you want on the original window
puts driver.title
#2
0
I used following code, its helped me to open each link in new tab.
我使用了以下代码,它帮助我在新标签中打开每个链接。
@driver.get "http://thiyagarajan.wordpress.com/"
link = @driver.find_elements(:tag_name, "a")
link.each do |a|
a = @driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", a)
a.click
end
#1
1
To switch to the popups, you would use:
要切换到弹出窗口,您可以使用:
driver.switch_to().window(handle)
So could do something like this:
所以可以做这样的事情:
#Get all of the window handles
i = driver.window_handles
#Iterate through the popups
#Note that starting at index 1 assuming we do not care about the original window
i[1..i.length].each do |handle|
#Switch to popup
driver.switch_to().window(handle)
#Do whatever processing you want on the popup
#Example, get the number of input fields
inputs = driver.find_elements(:tag_name, 'input')
puts inputs.length
#Probably want to close the popup
driver.close
end
#Switch back to the original window
driver.switch_to().window(i[0])
#Do whatever processing you want on the original window
puts driver.title
#2
0
I used following code, its helped me to open each link in new tab.
我使用了以下代码,它帮助我在新标签中打开每个链接。
@driver.get "http://thiyagarajan.wordpress.com/"
link = @driver.find_elements(:tag_name, "a")
link.each do |a|
a = @driver.execute_script("var d=document,a=d.createElement('a');a.target='_blank';a.href=arguments[0];a.innerHTML='.';d.body.appendChild(a);return a", a)
a.click
end