Open a Selenium browser with my cookies - ruby

I'm trying to create an automated script that goes to a website (Yik Yak) and submits stuff. It needs to access my cookies to know that I logged in before. It requires entering a key from my phone, and I can't automate that.
require 'selenium-webdriver'
profileDir = File.absolute_path("/home/carson/.mozilla/firefox/237ie3yd.default")
profile = Selenium::WebDriver::Firefox::Profile.from_name profileDir
driver = Selenium::WebDriver.for :firefox, :profile => profile
driver.navigate.to "https://www.yikyak.com/nearby/new"
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
element = driver.find_element(:class, 'form-control')
element.send_keys "Tessttt"
element.submit
It runs and opens Firefox, but it stops at the page where I have to enter the key my phone gets.
Any help?

default_profile = Selenium::WebDriver::Firefox::Profile.from_name "default"
default_profile.native_events = true
driver = Selenium::WebDriver.for(:firefox, :profile => default_profile)
Via Ruby Bindings

Related

How to work with a proxy in Firefox with Selenium WebDriver

I have code with the Chrome browser and this works, but I never worked with Firefox, but an Ubuntu server normally works only with Firefox, and now I have a question: How can I work with a proxy on the Firefox browser using the proxy_chain_rb gem?
I think my code for the Chrome browser will work in Firefox if you tell me how I can make Firefox options. My problem - I don’t know how I can use Firefox options and manuals are old. How can I replace my code for Google in Firefox?
Code
require 'watir'
require 'proxy_chain_rb'
require 'selenium-webdriver'
time2 = Time.now
file = File.new("report.json", "a:UTF-8")
myuseragent = File.readlines("user_agents.txt").sample
options = Selenium::WebDriver::Chrome::Options.new
options.add_emulation(user_agent: (myuseragent))
options.add_argument('--headless')
puts "Work started: " + time2.inspect
u_proxy = File.readlines("proxy.txt").sample
real_proxy = u_proxy
server = ProxyChainRb::Server.new
generated_proxy = server.start(real_proxy)
proxy = {
http: generated_proxy,
ssl: generated_proxy
}
caps = Selenium::WebDriver::Remote::Capabilities.chrome(:proxy => proxy)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps, options: options
driver.execute_script('return navigator.userAgent')
driver.get "https://raskruty.ru/"

How to run multiple firefox browser parallely having different proxy in Watir

The code is given Below , where it is launching three firefox browser
, all browser has different proxy settings. Using watir how launch all three browser same time using tread in watir???
require 'selenium-webdriver'
require 'rubygems'
require 'watir'
require 'rautomation'
require './CLReport.class'
require 'win32ole'
# TO INITIATE FIRST FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8080', :ssl => 'myproxy.com:8080'
$b1 = Watir::Browser.new :firefox, :profile => profile
$b1.goto("https://google.com")
# TO INITIATE SECOND FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8081', :ssl => 'myproxy.com:8081'
$b2 = Watir::Browser.new :firefox, :profile => profile
$b2.goto("https://google.com")
# TO INITIATE THORD FIREFOX BROWSER
# THE PROXY DATA CAN BE parameterized from Excel sheet
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'myproxy.com:8082', :ssl => 'myproxy.com:8082'
$b3 = Watir::Browser.new :firefox, :profile => profile
$b3.goto("https://google.com")
Now my question is how to join $b1,$b2,$b3 in a single browser using thread so that
only $browser.link(:text, "form application")click should work for all three browser parallely insted of writing
$b1.link(:text, "form application").click
$b2.link(:text, "form application").click
$b3.link(:text, "form application").click
i.e single line of code work work in three firefox browser same time parallely.
It is not possible because $b1,$b2,$b3 are instances of different browser,You can not make them equal.What are you doing is right. Or You can do some thing like that.
array = [$b1,$b2,$b3]
array.each { |browser|
browser.link(:text, "form application").click
}

How do I write a ruby web crawler which uses chrome?

I have a ruby web crawler that is currently coded to run in firefox. How do I switch it over to Chrome instead?
def open_browser()
tweaked_profile = Selenium::WebDriver::Firefox::Profile.new
tweaked_profile['nglayout.initialpaint.delay'] = 0
tweaked_profile.assume_untrusted_certificate_issuer=false
tweaked_profile['permissions.default.image'] = 2
tweaked_profile['network.proxy.type'] = 1
tweaked_profile['network.proxy.http'] = 'ec2proxy.csnzoo.com'
tweaked_profile['network.proxy.http_port'] = 8080
driver = Selenium::WebDriver.for :firefox, :profile => tweaked_profile
$browser = Watir::Browser.new(driver)
end
Should I just ditch watir and go with chromedriver or will watir work for this?
Check out http://watirwebdriver.com/chrome/, which has this example:
profile = Selenium::WebDriver::Chrome::Profile.new
...
b = Watir::Browser.new :chrome, :profile => profile
Also, these SO questions provide alternatives for crawling sites: Web crawler in ruby and What are some good Ruby-based web crawlers?

unable to locate element, using {:id=>"", :tag_name=>"select"}

I want to select value from drop down box using Ruby with watir-webdriver. Here is the command
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").select("Whiskey")
and i got an error
unable to locate element, using {:id=>"ctl00_SampleContent_ComboBox1_ComboBox1_OptionList", :tag_name=>"select"}
Any ideas what is wrong?
Here is full code:
# 1.Open http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx
#browser = Watir::Browser.new
#browser = Watir::Browser.new :ie
profile = Selenium::WebDriver::Firefox::Profile.from_name 'WatirWebDriver'
#profile.add_extension 'autoauth-2.1-fx+fn.xpi'
browser = Watir::Browser.new :firefox, :profile => profile
browser.goto 'http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/Default.aspx'
#2.Click ComboBox link on the left pane of the page
browser.a(:id, 'ctl00_SamplesLinks_ctl15_SamplesLink').click
#3.Verify that http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened
if browser.url.eql? "http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx"
puts "Error loading page \"http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/ComboBox/ComboBox.aspx URL opened\""
return false
end
#4.Select “Whiskey” in the combo-box
#browser.select_list(:id, 'ctl00_SampleContent_ComboBox1_ComboBox1_OptionList').select_value('Whiskey')
puts "!!!"
browser.select_list(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").when_present.select("Whiskey")
This does the job:
require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/ComboBox/ComboBox.aspx"
browser.button(id: "ctl00_SampleContent_ComboBox1_ComboBox1_Button").click
browser.ul(:id, "ctl00_SampleContent_ComboBox1_ComboBox1_OptionList").li(text: "Whiskey").click

Firefox 4 with watir webdriver: Need help using helperApps.neverAsk to save CSV without prompting

I learned how to use Firefox 4 with watir and webdriver (on Win7 x64), setting profile items. Example:
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = 'D:\\FirefoxDownloads'
profile["browser.helperApps.neverAsk.saveToDisk"] = "application/csv"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)
What I try to do with the example below, is setting CSV files to be always downloaded to a specific directory, never opened.
The code above succeeds in setting all the files automatically downloaded to the specified directory, but setting browser.helperApps.neverAsk.saveToDisk has no effect: I still get the open/save question.
After the script runs, the Firefox window is still open, and I enter the URL about:config.
I can see that browser.helperApps.neverAsk.saveToDisk was correctly set to application.csv , but in firefox/options/options/applications I don't see the entry for CSV files.
It seems that the menu setting, that is really effective, is not really bound with the about:config setting.
What am I doing wrong?
I've done some testing of this for you, unfortunately there doesn't seem to be a standard content-type for CSV files. You can try passing a comma separated list of content-types, hopefully one of those work for you. For me it was application/octet-stream that did the trick...
require 'watir-webdriver'
require 'selenium-webdriver'
profile = Selenium::WebDriver::Firefox::Profile.new
profile["browser.download.useDownloadDir"] = true
profile["browser.download.dir"] = '/tmp'
profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, text/csv, text/comma-separated-values, application/octet-stream"
driver = Selenium::WebDriver.for :firefox, :profile => profile
browser = Watir::Browser.new(driver)
browser.goto "http://altentee.com/test/test.csv"
In Firefox 6+, I couldn't get this to work without specifically setting the 'browser.download.folderList' value:
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.folderList'] = 2 #custom location
profile['browser.download.dir'] = download_directory
profile['browser.helperApps.neverAsk.saveToDisk'] = "text/csv, application/csv"
b = Watir::Browser.new :firefox, :profile => profile
See: http://watirwebdriver.com/browser-downloads/

Categories

Resources