Home > OS >  Ruby/Selenium access Yahoo Finance Quote Lookup field
Ruby/Selenium access Yahoo Finance Quote Lookup field

Time:04-17

I am trying to scrape information from Yahoo Finance website using Ruby and Selenium.

I need to locate Quote Lookup input field on the page and send it some value, like TWTR, to open/access information about Twitter company, for example.

This is what I have, but I receive error:

Code:

require 'selenium-webdriver'
require 'byebug'

target_asset = 'TWTR'
url = 'https://finance.yahoo.com/'

driver = Selenium::WebDriver.for :chrome
begin
  driver.get url

  sleep rand(2..4)

  input_element = driver.find_element(class: 'D(ib) Pstart(10px) Bxz(bb) Bgc($lv3BgColor) W(100%) H(32px) Lh(32px) Bdrs(0) Bxsh(n) Fz(s) Bg(n) Bd O(n):f O(n):h Bdc($seperatorColor) Bdc($linkColor):f finsrch-inpt').send_keys target_asset, :return


ensure
  driver.quit
end

But this is the error I get:

target frame detached (Selenium::WebDriver::Error::WebDriverError)

(Session info: chrome=100.0.4896.127)

CodePudding user response:

You must form the better locator, It works for me with the following locator. Try it out. You don't need any sleep statement because program automatically waits for page load. If you still want to use wait, you could wait implicit wait or explicit wait.

Write the following code, it works fine.

require 'selenium-webdriver'

target_asset = 'TWTR'
url = 'https://finance.yahoo.com/'
driver = Selenium::WebDriver.for :chrome
begin
  driver.get url
  driver.find_element(css: "input[placeholder='Quote Lookup']").send_keys target_asset, :return
  wait = Selenium::WebDriver::Wait.new(timeout: 30) # seconds
  wait.until { !driver.find_element(xpath: "//td[@data-test='MARKET_CAP-value']").text.empty? }
  p driver.find_element(xpath: "//td[@data-test='MARKET_CAP-value']").text
ensure
  driver.quit
end
  • Related