
IFrame(インラインフレーム)というWebサイト内に別のURLを読み込んで表示するHTMLタグです。
Pythonでは、このIFrame側のwebdriverを使ってブラウザ内のボタンを押す事が出来ますが、このIFrame側のボタンを押すには1手間が必要です。
PythonでChrome内のIFrame側のボタンをクリックする方法
dive = webdriver.Chrome()
dive.get('*****')
frame=dive.find_element_by_xpath('******')
dive.switch_to.frame(frame)
#IFrameの中のボタンをクリック
elem = dive.find_element_by_xpath('****')
elem.click()
print("IFrameの中のボタンをクリック")
time.sleep(10)
dive.switch_to.default_content()
■操作説明
frame=dive.find_element_by_xpath(‘******’)
まず、xpathで操作するIFrameの要素を取得します。
dive.switch_to.frame(frame)
操作するIFrameをChromeDriverにセットします。
これで以降のChromeDriver操作はIFrameの中を操作します。
dive.switch_to.default_content()
操作する対象をIFrame側から元に戻します。