Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NODRIVER] Cross-origin iframes not working #1928

Closed
RandomStrangerOnTheInternet opened this issue Jun 21, 2024 · 5 comments
Closed

[NODRIVER] Cross-origin iframes not working #1928

RandomStrangerOnTheInternet opened this issue Jun 21, 2024 · 5 comments

Comments

@RandomStrangerOnTheInternet

Hello, currently it seems that cross-origin iframes are not interactable by select_all, find. Would love to be able to interact with this, and I'm hoping someone is able to provide some assistance with this (I've checked all current issues, but I was not able to find a solution that works).

Below is a code-snippet in order to reproduce this.

import nodriver
import os


async def main():
	config = nodriver.Config()
	config.add_argument('--disable-web-security')
	config.user_data_dir = os.path.join(os.getcwd(), 'test')
	browser = await nodriver.start(config=config)

	page = await browser.get('https://csreis.github.io/tests/cross-site-iframe.html')

	cross_site_button = await page.find("Go cross-site (complex page)", best_match=True)
	await cross_site_button.mouse_click()

	search = await page.select_all('p', include_frames=True)
	print('Total number of p tags:', len(search))

	await page.sleep(60 * 60)


if __name__ == "__main__":
	nodriver.loop().run_until_complete(main())
@RandomStrangerOnTheInternet
Copy link
Author

Does anyone happen to have a solution regarding this?

@pythonlw
Copy link

`import json
from nodriver import start, cdp, loop
import nodriver as uc

async def switch_to_frame(browser, frame):
"""
change iframe
let iframe = document.querySelector("YOUR_IFRAME_SELECTOR")
let iframe_tab = iframe.contentWindow.document.body;
"""
iframe_tab: uc.Tab = next(
filter(
lambda x: str(x.target.target_id) == str(frame.frame_id), browser.targets
)
)
return iframe_tab

async def main():
browser_args = ['--disable-web-security']
browser = await uc.start(browser_args=browser_args)
tab = browser.main_tab
tab = await browser.get("http://www.yescaptcha.cn/auth/login")
for _ in range(10):
await tab.scroll_down(50)
await tab
await tab.sleep(15)

#solve Ordinary iframe
# query_selector = await tab.select_all('button[class="widgetLabel moveFromRightLabel-enter-done"]', include_frames=True)
# print('query_selector:', query_selector)
# if len(query_selector) == 1:
#     await query_selector[0].click()

#solve  Cross-origin iframe
recaptcha0 = await tab.select('iframe[title="reCAPTCHA"]')
print('recaptcha0:', recaptcha0.frame_id)
# # for tar in browser.targets:
# #     print('target_id:', tar.target.target_id)
iframe_tab = await switch_to_frame(browser, recaptcha0)
print('iframe_tabwebsocket_url:', iframe_tab.websocket_url)
iframe_tab.websocket_url = iframe_tab.websocket_url.replace("iframe", "page")
button = await iframe_tab.select("span#recaptcha-anchor")
await button.click()
input('stop')

if name == "main":
loop().run_until_complete(main())`

@pythonlw
Copy link

image

@ultrafunkamsterdam
Copy link
Owner

browser = await start(browser_args=['--disable-web-security', '--disable-site-isolation-trials'])

@fred-bao
Copy link

The problem is iframe is not in browser.targets, so that it can not be shifted, how this can be fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants