如何控制和访问特定的iframe?

时间:2019-04-16 16:02:39

标签: python selenium iframe sendkeys

我有些困惑,我一直在尝试使用python和selenium向下滚动网页。问题是似乎有一个iframe。我做了一些研究,以某种方式能够检测到iframe,但无法激活它:

pge ='https://www.google.com/search?rlz=1C1GCEU_en__835__835&ei=gfW0XKjNFeXjkgW8g6PYDA&q=huawei%20stores%20italy&oq=huawei+stores+italy&gs_l=psy-ab.3..0i22i30l3.4596.6067..6522...1.0..0.99.530.7......0....1..gws-wiz.......0i71j0i20i263j0i67j0j33i160.QYVpoia0BL4&npsic=0&rflfq=1&rlha=0&rllag=45499827,9211657,4837&tbm=lcl&rldimm=17275164572016510107&lqi=ChNodWF3ZWkgc3RvcmVzIGl0YWx5IgOIAQFaDwoNaHVhd2VpIHN0b3Jlcw&ved=2ahUKEwiKrMm8g9PhAhXQ4KQKHY9KDc8QvS4wAXoECAoQHQ&rldoc=1&tbs=lrf:!2m1!1e3!2m1!1e16!3sIAE,lf:1,lf_ui:4#rlfi=hd:;si:17275164572016510107,l,ChNodWF3ZWkgc3RvcmVzIGl0YWx5IgOIAQFaDwoNaHVhd2VpIHN0b3Jlcw;mv:!1m2!1d45.5258666!2d9.274078399999999!2m2!1d45.443338999999995!2d9.1152195;tbs:lrf:!2m1!1e3!2m1!1e16!3sIAE,lf:1,lf_ui:4'

/sbin:/usr/sbin:/bin:/usr/bin

当我执行上面的代码时,我可以看到向下滚动发生在后台窗口中,而不是前面的弹出窗口...

如果有人可以提供帮助,将不胜感激...我尝试了很多事情,例如使用控制台和Web扩展插件来找到iframe,但没有运气,我真的很困:-(

最良好的祝愿

更新: 嗨,大家好,有人建议将此问题作为重复的问题,但是当我尝试重复的解决方案时,它不起作用(我实际上在发布之前已经尝试过,并且也已经在原始代码中发布了它。)

SCROLL_PAUSE_TIME = 2
CYCLES=10
browser = webdriver.Firefox(firefox_options=opt)
browser.get(pge)

comment_button = browser.find_elements_by_class_name('Ob2kfd') 
sleep(5)
print("clicking on on link")
comment_button[0].click() 
sleep(2)
html = browser.find_element_by_tag_name('html')

# This does not work unfortunately:
# frames = browser.find_element_by_tag_name('iframe')
# browser.switch_to.frame(frames)

for i in range(CYCLES):
    html.send_keys(Keys.DOWN)
    time.sleep(SCROLL_PAUSE_TIME)

我收到以下错误:

SCROLL_PAUSE_TIME = 2
CYCLES=10
browser = webdriver.Firefox(firefox_options=opt)
browser.get(pge)

comment_button = browser.find_elements_by_class_name('Ob2kfd') 
sleep(5)
print("clicking on on link")
comment_button[0].click() 
sleep(2)
html = browser.find_element_by_tag_name('html')

# This does not work unfortunately:
# frames = browser.find_element_by_tag_name('iframe')
# browser.switch_to.frame(frames)

frames = browser.find_element_by_tag_name('iframe')
browser.switch_to.frame(0)

for i in range(CYCLES):
    html.send_keys(Keys.DOWN)
    time.sleep(SCROLL_PAUSE_TIME)

0 个答案:

没有答案