Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Finding nested elements in cdp mode + iframe #3848

Answered by mdmintz
alerodm asked this question in Q&A
Discussion options

Hey all

So I have the html below which is a simplification of my actual scenario. My goal is to iterate the li elements and extract the text and some attributes of the nested elements that it might have.

This is what I’m doing:

driver = SeleniumBaseDriver(browser="chrome", headless=False, uc=True, do_not_track=True)
driver.uc_activate_cdp_mode("file://path/to/html")
iframe = driver.cdp.find_element("iframe")
li_list = iframe.query_selector_all('li’)
print(list_list[0].query_selector(‘div’).text) # fails

I couldn't find a switch_to_frame equivalent in cdp mode, so I took the path of nested element finding instead. This works fine for locating the li elements, but I can't find anything nested within them, like the div. In these cases query_selector always returns None, though I can get the text or html of the li element itself.

Should I be taking a different approach? Thanks!

I'm on Chrome 137 and selenium-base 4.39.3

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <title>Selenium Test Page</title>
</head>
<body>
 <h1>Main Page</h1>
 <p>This is the main page content.</p>
 
 <iframe id="test-iframe" name="testFrame" width="600" height="400" srcdoc="<!DOCTYPE html>
 <html>
 <head>
 <title>Iframe Content</title>
 </head>
 <body>
 <h2>Iframe Content</h2>
 <ul id='item-list'>
 <li id='item-1' class='list-item'>
 <div data-testid='content-1' data-value='alpha'>First item content</div>
 </li>
 <li id='item-2' class='list-item'>
 <div data-testid='content-2' data-value='beta'>Second item content</div>
 </li>
 <li id='item-3' class='list-item'>
 <div data-testid='content-3' data-value='gamma'>Third item content</div>
 </li>
 <li id='item-4' class='list-item'>
 <div data-testid='content-4' data-value='delta'>Fourth item content</div>
 </li>
 <li id='item-5' class='list-item'>
 <div data-testid='content-5' data-value='epsilon'>Fifth item content</div>
 </li>
 </ul>
 </body>
 </html>">
 </iframe>
 
 <p>Content after the iframe.</p>
</body>
</html>```
You must be logged in to vote

I would check out #3528 (comment).
If element.query_selector(selector) isn't working and sb.get_nested_element(parent_selector, selector) isn't working, then you may have to call sb.reconnect() in order to use the regular Selenium iframe methods such as switch_to_frame(frame).

For some cases where you need to retain stealth, there are workarounds, specifically for interacting with elements inside nested iframes. PyAutoGUI can be used for that. Example: #3533 (comment).

Replies: 1 comment 2 replies

Comment options

I would check out #3528 (comment).
If element.query_selector(selector) isn't working and sb.get_nested_element(parent_selector, selector) isn't working, then you may have to call sb.reconnect() in order to use the regular Selenium iframe methods such as switch_to_frame(frame).

For some cases where you need to retain stealth, there are workarounds, specifically for interacting with elements inside nested iframes. PyAutoGUI can be used for that. Example: #3533 (comment).

You must be logged in to vote
2 replies
Comment options

Thanks Michael, the reconnection + switch_to_frame did the job. BTW, based on your experience, would you say moving back from the CDP mode to the regular UC mode would be a bad idea when dealing with AAA websites?

Comment options

It's probably all case-by-case, depending on the website. Sometimes UC Mode is good enough (eg. Google Search), and other times you need CDP Mode.

Answer selected by mdmintz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /