我是Selenium WebDriver的新手。出于好奇,我写了一个简单的代码。我制作了一个垃圾邮件机器人来打开一个页面,在在线用户列表中随机播放,然后按顺序点击它们中的每个人。发送一些消息,关闭新窗口并重复。
我在Selenium Firefox驱动程序上做了这个工作。看起来效果很好。但我想保持沉默,不是每次都打开firefox。所以我发现我可以通过PhantomJS做到这一点。
这是我的firefox工作代码:
IMammal
但是当我将Webdriver从Firefox切换到PhantomJS时,x.click方法不起作用。我做了一些测试,程序没有进一步点击方法。一段时间后,程序以(Net :: ReadTimeout)错误结束。
这是我最后一次尝试正常工作;
require 'selenium-webdriver'
def setup
@driver = Selenium::WebDriver.for :firefox
@reklam = 'Some testing message.'
end
def run
setup
@driver.get 'http://c2.me/okanb3'
first_window = @driver.window_handle
begin
@driver.switch_to.window(first_window)
@driver.find_element(link_text: "Shuffle").click
sleep 20
elements = @driver.find_elements(:class, 'shufflelink')
elements.each do |x|
x.click
all_windows = @driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
@driver.switch_to.window(new_window)
if @driver.page_source.include? 'The user is not available right now.' or @driver.page_source.include? 'User account is disabled.'
@driver.close
@driver.switch_to.window(first_window)
else
input = @driver.find_element(:id, 'inputbox')
input.send_keys(@reklam)
input.send_keys:return
@driver.close
popup = @driver.switch_to.alert
popup.accept
@driver.switch_to.window(first_window)
end
end
end while TRUE
end
run
正如你所看到的,我试图截取屏幕截图以了解问题,但截图似乎很好。这是:
编辑:版本:
require 'selenium-webdriver'
def setup
@driver = Selenium::WebDriver.for :phantomjs
@reklam = 'http://peyloride.com siteme beklerim.'
end
def teardown
@driver.quit
end
def run
setup
@driver.manage.window.resize_to(1920, 1080)
@driver.get 'http://c2.me/okanb3'
first_window = @driver.window_handle
begin
@driver.switch_to.window(first_window)
@driver.find_element(link_text: "Shuffle").click
puts "Shuffleandı"
sleep 20
elements = @driver.find_elements(:class, 'shufflelink')
elements.each do |x|
puts "click'e geldik"
@driver.save_screenshot "phantomjs.png"
x.click
@driver.save_screenshot "phantomjs2.png"
puts "click yaptı sonunda aq"
all_windows = @driver.window_handles
new_window = all_windows.select { |this_window| this_window != first_window }
@driver.switch_to.window(new_window)
if @driver.page_source.include? 'The user is not available right now.' or @driver.page_source.include? 'User account is disabled.'
@driver.close
@driver.switch_to.window(first_window)
else
input = @driver.find_element(:id, 'inputbox')
input.send_keys(@reklam)
input.send_keys:return
@driver.close
popup = @driver.switch_to.alert
popup.accept
@driver.switch_to.window(first_window)
end
end
end while TRUE
end
run