Capybara: locating iframes

661 Views Asked by At

I'm trying to program a bot to scrape and apply for jobs on indeed.com. My question is, how can i locate the id of an iframe so i can run commands within it.

  unless page.has_css?('p.expired')
    click_link('Apply Now')

    page.driver.within_frame(1) do
      page.driver.within_frame(0) do
        complete_step_one
        complete_additional_steps
      end

The frame that pops up is when you click on Apply Now, it asks for name, number, email, cover letter.

Sample Link: https://www.indeed.com/cmp/SMCI/jobs/Project-Engineer-3ebc5b1b2bf00349?sjdu=QwrRXKrqZ3CNX5W-O9jEvWIuBcfYv3mrYLqkE6HctuqxVGTbpbhVXnHKq24JPICkWaBy43U6kq7z7H-uMado3w&tk=1citcpf1rbi5485q&vjs=3

Any help is appreciated.

1

There are 1 best solutions below

0
Thomas Walpole On

within_frame takes the frame element, the name or id of the frame, the index of the frame on the page, or in the latest version of Capybara you can pass nothing if there's only one frame on the page.

From the link you pasted it appears the main document conatins one iframe that also contains one iframe, so that could be

page.within_frame(0) do
  page.within_frame(0) do
    ...  # do whatever in the inner frame
  end
end

or in the latest version of Capybara

page.within_frame do
  page.within_frame do
    ...  # do whatever in the inner frame
  end
end

Another way to do it would be something like

page.within_frame('indeedapply-modal-preload-iframe') do  # select outer iframe by id
  inner_frame = page.find('iframe[src^="https://apply.indeed.com/indeedapply/resumeapply?"]') # use CSS attribute starts with selector to find inner iframe
  within_frame(inner_frame) do # pass the found iframe element
    ...  # do whatever you want in the inner frame
  end
end