Latest / CyberCode Academy / Course 40 - Web Scraping with Python | Episode 36: Comprehensive Element Locating and Advanced Webpage Navigation
Transcript
- 0:00Speaker 1: So imagine you've got this this digital assistant, and it
- 0:03Speaker 1: literally just sits at your computer, opens up a web browser,
- 0:06Speaker 1: and you does all your digital chores for you.
- 0:09Speaker 2: Oh, that would be amazing.
- 0:11Speaker 1: Right, I'm talking about like filling out those super tedious
- 0:13Speaker 1: multi page onboarding forms, or clicking through just endless confirmation buttons,
- 0:18Speaker 1: or even scouring databases for specific information, all at lightning
- 0:22Speaker 1: speed while you just go grab a coffee.
- 0:25Speaker 2: Yeah. I mean, it sounds like science fiction or at least,
- 0:27Speaker 2: you know, a very expensive enterprise software solution that most
- 0:30Speaker 2: people wouldn't have access to.
- 0:32Speaker 1: Exactly, But it's actually just standard web automation, it is.
- 0:35Speaker 2: And the crazy thing about it is to pull this off,
- 0:38Speaker 2: you kind of have to completely unlearn how you view
- 0:40Speaker 2: the Internet. What do you mean, Well, to a piece
- 0:43Speaker 2: of automation code, the Internet has no colors, it has
- 0:46Speaker 2: no shiny buttons, no pretty layouts. To automate the web,
- 0:50Speaker 2: we have to look past the user interface.
- 0:54Speaker 1: Entirely, right, You're interacting directly with the raw architecture underneath exactly.
- 0:58Speaker 2: You're dealing with the skeleton of the site, not the
- 1:01Speaker 2: paint job.
- 1:01Speaker 1: So today, for this deep dive, we are basically going
- 1:05Speaker 1: to act as the translators between human intention and machine execution.
- 1:10Speaker 1: We're going to walk you through step by step how
- 1:12Speaker 1: a script actually parses and manipulates the web page.
- 1:16Speaker 2: Yeah, and we'll be talking specifically about using tools like
- 1:20Speaker 2: Selenium and uh something called Chrome Driver.
- 1:24Speaker 1: Right, So let's start with the ignition key. Basically, to
- 1:26Speaker 1: have an invisible assistant click on anything, we need a
- 1:29Speaker 1: way for our Python code to talk to the browser
- 1:31Speaker 1: in the first place.
- 1:32Speaker 2: Yeah, and that is exactly the role of the Chrome Driver.
- 1:35Speaker 2: I mean, think of it as an executable application that
- 1:37Speaker 2: acts as a.
- 1:38Speaker 1: Bridge, okay, a bridge between what and what.
- 1:40Speaker 2: Between your script and the browser. So your script sends
- 1:44Speaker 2: commands to the driver, and then the driver translates those
- 1:47Speaker 2: commands into native browser actions.
- 1:49Speaker 1: Got it.
- 1:50Speaker 2: When you instantiate this driver in your code, an actual
- 1:53Speaker 2: instance of Chrome boots up on your machine, but it's
- 1:57Speaker 2: entirely controlled by your script. You can literally sit there
- 1:59Speaker 2: and watch it move on on its own, which.
- 2:00Speaker 1: Is always a little spooky the first time you see it.
- 2:02Speaker 2: It really is.
- 2:03Speaker 1: Now, one of the first things a developer typically does
- 2:06Speaker 1: after booting that instance is they pass an argument to
- 2:09Speaker 1: maximize the browser window right away.
- 2:11Speaker 2: Yes, that is a crucial step, but on.
- 2:14Speaker 1: The surface that seems I don't know, purely cosmetic, like
- 2:19Speaker 1: why would the script care how big the window is
- 2:21Speaker 1: if it's just reading the underlying code anyway?
- 2:23Speaker 2: Well, functionally, it's all about responsive design. I mean, modern
- 2:27Speaker 2: web pages are built to change their structural layout based
- 2:30Speaker 2: on the size of the screen you're using.
- 2:31Speaker 1: Oh right, Like sites look different on a phone versus
- 2:34Speaker 1: a desktop exactly.
- 2:36Speaker 2: So if you load a complex web app in a tiny,
- 2:39Speaker 2: minimized window, the site's code might dynamically hide at a
- 2:42Speaker 2: navigation menu. It might shove it behind one of those
- 2:46Speaker 2: mobile style Hamburger icons.
- 2:48Speaker 1: Oh wow, I see where this is going.
- 2:50Speaker 2: Yeah, So if your script is hunting for a specific
- 2:53Speaker 2: settings button that has suddenly been hidden or removed from
- 2:56Speaker 2: the active.
- 2:57Speaker 1: Layout, the automation just crashes, right, It.
- 2:59Speaker 2: Throws an error because the button isn't there anymore. So
- 3:02Speaker 2: maximizing the window ensures the layout is predictable, It ensures
- 3:06Speaker 2: all the standard desktop elements are rendered and you know,
- 3:10Speaker 2: actually available to interact with.
- 3:12Speaker 1: That makes total sense. So we've set the stage, we've
- 3:15Speaker 1: maximized the window, and we've used to command like a driver,
- 3:18Speaker 1: do get to navigate to a specific url.
- 3:21Speaker 2: Yep, the page loads.
- 3:22Speaker 1: Now to a human user, a search bar is obvious.
- 3:25Speaker 1: It's a white rectangle with a little magnifine glass next
- 3:27Speaker 1: to it.
- 3:28Speaker 2: Right, you just click it.
- 3:28Speaker 1: But since our script doesn't have eyes, how does it
- 3:31Speaker 1: actually pinpoint that specific interactive element out of like thousands
- 3:37Speaker 1: of lines of raw HTML code.
- 3:39Speaker 2: It relies entirely on the Document object model, or the
- 3:44Speaker 2: DOM as most people call it the DOM. Okay, Yeah,
- 3:46Speaker 2: when a browser loads a web page, it takes the
- 3:49Speaker 2: raw HTML text and turns it into this giant inverted tree.
- 3:52Speaker 1: Structure, like a family tree sort of.
- 3:54Speaker 2: Yeah, the page itself is the trunk, and every container,
- 3:58Speaker 2: every paragraph, every button is a brand or a leaf
- 4:00Speaker 2: on that tree.
- 4:01Speaker 1: Okay, I'm visualizing that.
- 4:02Speaker 2: So the script navigates this tree. If you want to
- 4:05Speaker 2: find a specific element, you need a locator strategy, and
- 4:09Speaker 2: the absolute most reliable strategy is looking for a unique
- 4:12Speaker 2: identifier that the original developers left behind.
- 4:15Speaker 1: It's kind of like giving our script the exact GPS
- 4:18Speaker 1: coordinates of a specific house in a sprawling city.
- 4:20Speaker 2: That is a perfect analogy.
- 4:22Speaker 1: Actually, so if the developers built the site, well, they
- 4:24Speaker 1: assign these unique attributes to elements.
- 4:27Speaker 2: Yeah.
- 4:27Speaker 1: Like if you use a browsers and spectool on a
- 4:31Speaker 1: well designed site's search bar, you might see an attribute
- 4:34Speaker 1: called ID, right, like i'd search field.
- 4:37Speaker 2: Yes, and an ID is the gold standard for locators.
- 4:40Speaker 2: Why is it the gold standard because by definition in
- 4:43Speaker 2: standard HTML rules, an ID is supposed to be entirely
- 4:47Speaker 2: unique on a single web page. It is literally like
- 4:49Speaker 2: a social security number for a web element.
- 4:52Speaker 1: Wow. Okay, so it's foolproof pretty much.
- 4:54Speaker 2: If you command your script to findelement bide and pass
- 4:58Speaker 2: it that specific string that driver searches, the dom tree
- 5:01Speaker 2: instantly locks onto that specific node and completely ignores the
- 5:04Speaker 2: rest of the page.
- 5:05Speaker 1: That's super efficient. But what if the developer didn't use
- 5:09Speaker 1: an ID, because we all know not every site is
- 5:12Speaker 1: coded perfectly.
- 5:13Speaker 2: Oh absolutely, it happens all the time.
- 5:15Speaker 1: Like sometimes you inspect an element and it just has
- 5:17Speaker 1: a name attribute, Like the main search box on a
- 5:19Speaker 1: coding tutorial site might just have the name attribute assigned
- 5:22Speaker 1: to the letter Q.
- 5:23Speaker 2: Right. Q usually stands for a query and the fidelment
- 5:28Speaker 2: by name command works very similarly to ID. It's incredibly fast.
- 5:32Speaker 1: But I hear a butt coming.
- 5:34Speaker 2: But name attributes aren't strictly required to be unique.
- 5:38Speaker 1: Ah, there it is.
- 5:39Speaker 2: Yeah. So if a developer got sloppy and named both
- 5:42Speaker 2: the top search bar and say a bottom newsletter sign
- 5:46Speaker 2: up form Q.
- 5:47Speaker 1: Then your script might grab the.
- 5:49Speaker 2: Wrong one exactly, because the script generally just defaults to
- 5:52Speaker 2: the very first match and encounters as it scans the
- 5:54Speaker 2: document from top to bottom.
- 5:55Speaker 1: Oh right, so it hits the first que, says found it,
- 5:58Speaker 1: and stops looking. Okay, so IDs and names are the
- 6:02Speaker 1: easy street addresses, But we also have to navigate around
- 6:05Speaker 1: the site. Say I want the script to click a
- 6:07Speaker 1: specific text link that just says my courses.
- 6:10Speaker 2: Well, Selenium provides a findelement by link text method for
- 6:14Speaker 2: exactly this scenario. Oh nice, Yeah, you just passed the
- 6:17Speaker 2: text of the link to the script, and it hunts
- 6:19Speaker 2: for an anchor tag in the HTML that contains that
- 6:21Speaker 2: exact string.
- 6:23Speaker 1: I imagine that demand's absolute precision, though.
- 6:25Speaker 2: Oh it really does.
- 6:26Speaker 1: It's like telling a taxi driver to take you to
- 6:28Speaker 1: the Grand Hotel and they flat out refuse to move
- 6:32Speaker 1: because the physical sign on the building actually says the
- 6:35Speaker 1: Grand Hotel Incorporated.
- 6:36Speaker 2: Yes, the machine has zero intuition. It is executing a
- 6:41Speaker 2: literal string comparison.
- 6:43Speaker 1: So if I tell the script to look for my courses,
- 6:46Speaker 1: but the web designer capitalized it to my courses, yeah,
- 6:49Speaker 1: in all caps.
- 6:50Speaker 2: The script will just crash. It'll throw a no such
- 6:52Speaker 2: element exception, A single mismatch space, an invisible line break,
- 6:57Speaker 2: or different capitalization will break it.
- 6:59Speaker 1: That sounds incredibly fragile.
- 7:00Speaker 2: It can be. But to mitigate that fragility, you can
- 7:03Speaker 2: use a variation called partial link text.
- 7:06Speaker 1: Oh, or you only need a piece of it exactly.
- 7:08Speaker 2: You only match a substring. So if the link is
- 7:10Speaker 2: a dynamically generated product title like blue Sneaker size ten,
- 7:14Speaker 2: ID four four five nine, you can just target the
- 7:17Speaker 2: blue Sneakers portion.
- 7:18Speaker 1: Okay, So that ensures the script finds it even if
- 7:21Speaker 1: the ID numbers or the sizes change tomorrow.
- 7:23Speaker 2: Right. It gives you a lot more flexibility.
- 7:25Speaker 1: Got it. So far we've relied on developers giving us
- 7:29Speaker 1: clean IDs, names or exact text links. But what if
- 7:33Speaker 1: we're automating some highly complex, deeply nested web app where
- 7:37Speaker 1: the elements don't have any of those clean attributes.
- 7:39Speaker 2: Yeah, the tricky stuff.
- 7:41Speaker 1: How do we find the house if there's literally no
- 7:42Speaker 1: address on the mailbox?
- 7:44Speaker 2: This is where we shift from simple addresses to architectural blueprints.
- 7:48Speaker 2: When basic locators fail, we rely on advanced locators, specifically
- 7:52Speaker 2: CSS selectors and XPath Okay, let's.
- 7:55Speaker 1: Start with CSS now. CSS is what dictates the visual
- 7:57Speaker 1: styling of a page, right like colors, padding, Yes, exactly,
- 8:01Speaker 1: and developers apply these styles by assigning elements to classes.
- 8:04Speaker 1: So if a button is red and round, it might
- 8:06Speaker 1: have a class called primary submit btn. We can actually
- 8:10Speaker 1: hijack those styling hooks for automation.
- 8:12Speaker 2: You absolutely can. Using a CSS selector locator, you can
- 8:16Speaker 2: command the script to find an element based entirely on
- 8:19Speaker 2: how it was styled.
- 8:20Speaker 1: That's so clever.
- 8:21Speaker 2: Yeah, in your code, you use standard CSS syntax like
- 8:24Speaker 2: a dot to denote a class, so you'd tell the
- 8:26Speaker 2: driver to find dot primary dash submit dash btn.
- 8:30Speaker 1: And is that fast?
- 8:31Speaker 2: It is incredibly fast. The beauty of this is that
- 8:34Speaker 2: CSS selectors are heavily optimized by the browser itself.
- 8:37Speaker 1: But CSS classes still rely on the developer leaving a
- 8:41Speaker 1: recognizable hook. Sure, what if there was absolutely nothing unique
- 8:45Speaker 1: about the element itself, no class, no ID. That's where
- 8:48Speaker 1: XPath comes in, right, Yes, because I've seen complex XPath
- 8:51Speaker 1: strings and honestly, they look like absolute gibberish. It's just
- 8:54Speaker 1: like slashes, brackets and random HTML tags.
- 8:57Speaker 2: They do look intimidating. But XPath is the heavy artillery
- 9:00Speaker 2: of web automation. It stands for XML path language. Okay,
- 9:04Speaker 2: remember how we describe the dom as an inverted tree.
- 9:07Speaker 2: This path is a literal set of step by step
- 9:09Speaker 2: directions for navigating the branches of that tree. You can
- 9:12Speaker 2: use it to triangulate an element based entirely on its
- 9:14Speaker 2: relationship to other elements.
- 9:16Speaker 1: How does that actually map out in the code though?
- 9:18Speaker 2: Okay, imagine you want to click a specific input box,
- 9:22Speaker 2: but it has no ID, no name, and a generic class.
- 9:25Speaker 1: Basically a ghost element.
- 9:27Speaker 2: Right, But you notice it sits inside a larger container,
- 9:30Speaker 2: a divtag that does have an idea of login panel.
- 9:33Speaker 1: Okay, so the parent container has a clear address exactly.
- 9:37Speaker 2: Using XPath, you can write a path that says, scan
- 9:40Speaker 2: the document for a div with the ID log in panel.
- 9:44Speaker 2: Once you find it, step inside that container, find the
- 9:47Speaker 2: second paragraph, and give me the input box inside that paragraph. Wow,
- 9:51Speaker 2: it doesn't matter if the input box has no identifying
- 9:54Speaker 2: features of its own. You've located it purely based on
- 9:57Speaker 2: its surroundings.
- 9:58Speaker 1: It's literally like saying, go to the house house on
- 10:00Speaker 1: the corner of Fifth and Main, walk to the kitchen
- 10:02Speaker 1: and grab the second drawer on the left. Yes, you
- 10:05Speaker 1: don't need a label in the drawer if you know
- 10:06Speaker 1: the exact path to reach it.
- 10:08Speaker 2: That is the power of x path. It can navigate
- 10:11Speaker 2: down from parent to child, or even up from a
- 10:13Speaker 2: child to a parent, or sideways across sibling elements. It's
- 10:16Speaker 2: incredibly robust.
- 10:17Speaker 1: All right, So we've mastered finding elements using the dom tree.
- 10:21Speaker 1: But just pointing at a search bar doesn't execute a search.
- 10:24Speaker 1: We have to interact with it, right.
- 10:26Speaker 2: Once the driver locates the element, it stores it as
- 10:29Speaker 2: an object in your Python code, okay, and you can
- 10:31Speaker 2: then call methods on that object. So to type into
- 10:35Speaker 2: a field, you use the sen keys command. Is a
- 10:37Speaker 2: keys You pass it a string of texts, and the
- 10:39Speaker 2: driver basically simulates the electrical signals of a human physically
- 10:43Speaker 2: typing on a keyboard, sending those exact keystrokes directly into
- 10:47Speaker 2: the input node.
- 10:48Speaker 1: Wait if it just sends keystrokes blindly, what happens if
- 10:51Speaker 1: the search bar already has pre populated text, Like sometimes
- 10:55Speaker 1: you visit a portal and the search box already says
- 10:57Speaker 1: search by employee name and light gray text. If I
- 11:01Speaker 1: just fire off send keys with the name John, does
- 11:03Speaker 1: it just appendit like? Does it search for search by
- 11:07Speaker 1: employee name John.
- 11:08Speaker 2: It absolutely will do exactly that. Oh no, yeah, send
- 11:12Speaker 2: keys doesn't overwrite existing values, it appends to them. This
- 11:15Speaker 2: is a classic architectural trap.
- 11:17Speaker 1: Because humans instinctively recognize placeholder text right we backspace it
- 11:22Speaker 1: or we highlight it before typing.
- 11:24Speaker 2: Exactly, but the machine does not. Therefore, robust automation requires
- 11:28Speaker 2: explicit instruction. Before you ever send new keys to a
- 11:33Speaker 2: text input, you invoke the dot clear method yeap. This
- 11:37Speaker 2: commands the driver to wipe the input node clean of
- 11:40Speaker 2: any pre existing string values. You locate it, you clear it,
- 11:44Speaker 2: and only then do you send your keys.
- 11:46Speaker 1: Man, it really emphasizes how much micrologic goes into our
- 11:50Speaker 1: daily browsing habits. Stuff we don't even think about.
- 11:53Speaker 2: We take it totally for granted.
- 11:54Speaker 1: So we've cleared the field and typed our query. To
- 11:57Speaker 1: actually run the search, we need to get enter. How
- 12:00Speaker 1: do we do that?
- 12:01Speaker 2: You import a specific keys class in your script. This
- 12:03Speaker 2: class contains representations of all the non alphanumeric keyboard.
- 12:07Speaker 1: Keys, like shift, alt enter all that.
- 12:09Speaker 2: Exactly, So you just deppend keys, dot enter or keys
- 12:13Speaker 2: return to your sen keys command. The driver simulates that
- 12:16Speaker 2: specific keypress, the web server receives the form submission, and boom,
- 12:19Speaker 2: your automated search is running.
- 12:21Speaker 1: That is so cool. Now, this all works beautifully in
- 12:23Speaker 1: a vacuum. But let's look at the real world, where
- 12:26Speaker 1: the Internet is messy, connections are slow, and web apps
- 12:30Speaker 1: are incredibly complex.
- 12:31Speaker 2: Oh yes, the real world is a nightmare for automation, right.
- 12:35Speaker 1: What happens when our script encounters, say, a multi step
- 12:39Speaker 1: sign up process with a really heavy loading screen.
- 12:42Speaker 2: This is where we hit the fundamental friction of web automation.
- 12:46Speaker 2: It's the speed gap between the CPU and the network.
- 12:49Speaker 1: The speed gap.
- 12:50Speaker 2: Yeah, your Python script executes commands in fractions of a millisecond.
- 12:54Speaker 2: It tells the driver to click next. The browser sends
- 12:57Speaker 2: a request to the server, and the server takes say two,
- 13:00Speaker 2: two full seconds to respawn and draw the new page.
- 13:02Speaker 1: And during those two seconds, our script is already trying
- 13:05Speaker 1: to execute the next line of code right yep, which
- 13:07Speaker 1: is filling out the first name field on a page
- 13:10Speaker 1: that literally doesn't exist yet exactly.
- 13:12Speaker 2: The script checks the dom doesn't see the first name
- 13:15Speaker 2: element and instantly throws a fatal no such element exception.
- 13:19Speaker 2: It just crashes.
- 13:20Speaker 1: Okay, So the obvious brute force fix would be to
- 13:23Speaker 1: just tell the script a pause, like use a sleep
- 13:26Speaker 1: command to freeze the code for five seconds after every
- 13:29Speaker 1: single click, just to be safe.
- 13:31Speaker 2: You could do that, but static sleeps are a terrible
- 13:33Speaker 2: engineering practice.
- 13:34Speaker 1: Really why well, think about it.
- 13:36Speaker 2: If you tell the code to sleep for ten seconds
- 13:39Speaker 2: and the page loads in two seconds, you are wasting
- 13:42Speaker 2: eight seconds of compute time.
- 13:43Speaker 1: Oh true.
- 13:44Speaker 2: If you scale that script up to process a ten
- 13:47Speaker 2: thousand records, you've just added hours of dead air.
- 13:50Speaker 1: That's highly inefficient.
- 13:51Speaker 2: And conversely, if the server is having a slow day
- 13:54Speaker 2: and takes eleven seconds to load, your script still crashes anyway.
- 13:57Speaker 1: Oh man, Yeah, so we need a dynamic way to
- 14:00Speaker 1: bridge the CPU speed and then network speed.
- 14:03Speaker 2: Right, And the elegant solution here is something called an
- 14:05Speaker 2: explicit weight. It's often implemented as web driver weight.
- 14:09Speaker 1: How does that differ from just sleeping.
- 14:11Speaker 2: Instead of freezing the code entirely? You define a maximum
- 14:14Speaker 2: time out, say fifteen seconds, and you define a specific
- 14:17Speaker 2: condition like wait until the first name element is clickable.
- 14:21Speaker 1: So how does that look under the hood? Is it
- 14:23Speaker 1: just checking constantly?
- 14:24Speaker 2: It utilizes a polling mechanism. The driver hooks into the
- 14:28Speaker 2: process and checks the DOM tree every five hundred milliseconds.
- 14:32Speaker 2: Oh wow, it asks, is the element here yet?
- 14:34Speaker 1: No?
- 14:35Speaker 2: Half a second later, is it here yet? No? The
- 14:38Speaker 2: absolute millisecond. The server finishes rendering the element and attaches
- 14:43Speaker 2: it to the DOM. The driver detects it and then
- 14:45Speaker 2: he just goes instantly. It breaks the weight sequence and
- 14:48Speaker 2: executes the next command. It completely optimizes the script's efficiency.
- 14:52Speaker 1: That is brilliant. It ties the execution pace strictly to
- 14:56Speaker 1: the application's actual state, not just arbitrary guessing. Exactly all right,
- 15:00Speaker 1: So our weight command worked. The page is loaded and
- 15:03Speaker 1: we're looking at a registration form. Normal text fields are easy.
- 15:06Speaker 1: Now we locate clear and send keys. But then we
- 15:09Speaker 1: hit the country of residence field and it's a drop
- 15:12Speaker 1: down menus.
- 15:13Speaker 2: Oh, drop down menus. They reveal how deceptive graphical user
- 15:16Speaker 2: interfaces really are.
- 15:17Speaker 1: What do you mean?
- 15:18Speaker 2: As users? We just see a single seamless box that
- 15:20Speaker 2: we click and a nice list drops down. But the
- 15:23Speaker 2: script bypasses the UI completely. It looks at the HTML
- 15:26Speaker 2: document and sees a completely different structure.
- 15:28Speaker 1: Right, Because HTML strictly defines how data is nested. A
- 15:33Speaker 1: dropdown isn't just one element, is it. It's a parent
- 15:36Speaker 1: container holding dozens of child elements.
- 15:38Speaker 2: Precisely. It is typically built using a select tag for
- 15:42Speaker 2: the main container, and inside that container are individual option
- 15:46Speaker 2: tags for every single choice, one for Canada, one for
- 15:50Speaker 2: the UK, one for the United States.
- 15:52Speaker 1: So you can't just type into it.
- 15:53Speaker 2: No, you cannot just use send keys on the select
- 15:56Speaker 2: container because it's not a text input field.
- 15:59Speaker 1: So we have to somehow tell the script to dig
- 16:02Speaker 1: inside that container, sift through all the option tags and
- 16:05Speaker 1: pick the right one. How do we do that?
- 16:08Speaker 2: First you use a locator to isolate the main select
- 16:11Speaker 2: dropdown element. Once you have that object, you run a
- 16:13Speaker 2: command called finned elements plural with an s okay plural yes,
- 16:17Speaker 2: and you tell it to find every single child element
- 16:20Speaker 2: with the tag name option.
- 16:21Speaker 1: Because it's plural, it doesn't just stop at the first
- 16:23Speaker 1: match like before. It grabs every single one of them.
- 16:26Speaker 2: Exactly.
- 16:26Speaker 1: What does it do with all that data?
- 16:28Speaker 2: It extracts them from the dom and stores them in
- 16:30Speaker 2: your script's memory as a list like an array of
- 16:33Speaker 2: web element objects. So if there are one hundred and
- 16:36Speaker 2: ninety five countries in that dropdown, your script is now
- 16:38Speaker 2: holding an array of one hundred and ninety five distinct objects.
- 16:41Speaker 1: Okay, so the script has a massive bucket full of
- 16:44Speaker 1: country options. How does it isolate just the United States
- 16:48Speaker 1: option so it can click it.
- 16:49Speaker 2: You employ a standard programming loop, afore loop. You instruct
- 16:53Speaker 2: your code to iterate.
- 16:54Speaker 1: Over that array, so it just checks some one by one.
- 16:57Speaker 2: Basically, the script takes the first element, reads it's internal
- 17:00Speaker 2: text attribute, and asks a logical question, does this text
- 17:04Speaker 2: equal United States?
- 17:06Speaker 1: And if it doesn't?
- 17:07Speaker 2: If false, it discards it and checks the second element.
- 17:09Speaker 2: It loops through the entire array at the speed of
- 17:12Speaker 2: the processor, verifying the text of each option tag.
- 17:15Speaker 1: In the moment it hits the true condition. The moment
- 17:17Speaker 1: the text matches exactly, we trigger the doc click method
- 17:21Speaker 1: on that specific object exactly.
- 17:22Speaker 2: It scans the array, finds the match, executes the click,
- 17:25Speaker 2: and breaks the loop. The browser receives the click event
- 17:28Speaker 2: on that specific option note, and the drop down selection
- 17:31Speaker 2: updates on the screen.
- 17:32Speaker 1: It is a wildly methodical process to achieve what takes
- 17:36Speaker 1: a human like a flick of the wrist.
- 17:38Speaker 2: It really is.
- 17:39Speaker 1: It really pulls back the curtain on how complex the
- 17:42Speaker 1: underlying web is.
- 17:43Speaker 2: Ye.
- 17:44Speaker 1: I mean we've mastered finding elements, typing, waiting for a
- 17:48Speaker 1: synchrodous network loads, and looping through complex data arrays. Our
- 17:52Speaker 1: script must be practically unstoppable.
- 17:55Speaker 2: At this point you'd think so, wouldn't you. But modern
- 17:58Speaker 2: web architecture has a few security features and structural quarks
- 18:02Speaker 2: designed specifically to keep elements isolated. Uh oh, if you
- 18:05Speaker 2: aren't aware of them, your automation will hit an invisible wall.
- 18:08Speaker 1: I think I can guess one of these traps. Sometimes
- 18:10Speaker 1: you inspect an element, you write the absolutely perfect XPath,
- 18:13Speaker 1: you run the script, and it still throws a nos
- 18:15Speaker 1: element exception. It's like the element is a complete ghost.
- 18:18Speaker 2: Yes. When you encounter a ghost element like that, you
- 18:21Speaker 2: are almost certainly dealing with an iframe?
- 18:23Speaker 1: An iframe, inline frame? What is the actual architectural purpose
- 18:27Speaker 1: of an iframe?
- 18:27Speaker 2: Think of it as a strict security sandbox. An iframe
- 18:31Speaker 2: allows a developer to embed an entirely separate, independent HTML
- 18:35Speaker 2: document inside their main web page. Oh weird, Yeah, it
- 18:38Speaker 2: has its own isolated dom. You see this constantly with
- 18:41Speaker 2: third party content, like if a blog embeds a YouTube video.
- 18:46Speaker 2: That video player is living inside an iframe.
- 18:49Speaker 1: Okay, that makes sense.
- 18:50Speaker 2: Or if an e commerce site uses a third party
- 18:52Speaker 2: payment processor like Stripe to collect credit cards, those input
- 18:56Speaker 2: fields are hosted inside an iframe, so the main website's
- 18:59Speaker 2: code can't seek, at least scrape your credit card number.
- 19:02Speaker 1: It's literally a web page operating inside a web page.
- 19:05Speaker 1: It's web page inception.
- 19:06Speaker 2: And here is exactly why it breaks automation. When you
- 19:09Speaker 2: instantiate your driver, it operates entirely within the execution context
- 19:14Speaker 2: of the parent web pagees dom right, it is strictly
- 19:17Speaker 2: blind to the contents of any embedded iframes. It's like
- 19:20Speaker 2: standing in a hallway looking at a locked office door.
- 19:23Speaker 2: You know there is a room there, but you cannot
- 19:25Speaker 2: see the desks or the computers inside.
- 19:27Speaker 1: So if I try to command the script to click
- 19:29Speaker 1: the play button on that embedded video, it scans the hallway,
- 19:32Speaker 1: doesn't see a play button, and crashes. Exactly, how do
- 19:35Speaker 1: we give the script the key to the office door.
- 19:37Speaker 2: Then you must explicitly command the driver to switch its
- 19:40Speaker 2: execution context. You use a command like driver dot switch
- 19:44Speaker 2: toe dot frame, and you pass it the locator for
- 19:47Speaker 2: the iframe itself like its ID or its index number.
- 19:50Speaker 1: Okay.
- 19:50Speaker 2: Once that command executes, the driver effectively steps through the doorway.
- 19:54Speaker 2: It leaves the parent dom behind and maps out the
- 19:57Speaker 2: internal dom of the iframe. It can easily find and
- 20:01Speaker 2: click the play button.
- 20:02Speaker 1: That makes perfect sense. But knowing how rigid this logic is,
- 20:06Speaker 1: there has to be a catch when we want to
- 20:07Speaker 1: go back to the main web page. Right.
- 20:08Speaker 2: Oh, a massive catch. Once the driver steps into the iframe,
- 20:12Speaker 2: it becomes entirely blind to the hallway outside.
- 20:14Speaker 1: I knew it.
- 20:15Speaker 2: Yeah, if your script tries to go back to filling
- 20:18Speaker 2: out the main form without switching its context back, it
- 20:21Speaker 2: will crash. You must explicitly command the driver to switchtoe
- 20:24Speaker 2: dot default content to step back out into the parent dom.
- 20:27Speaker 1: Wow.
- 20:28Speaker 2: Context management is genuinely one of the hardest things to
- 20:31Speaker 2: debug in a complex automation script.
- 20:34Speaker 1: Note to self, never leave the script locked inside the
- 20:38Speaker 1: iframe room. Now what about pop ups? And I don't
- 20:43Speaker 1: mean HTML modals that are built into the page. I
- 20:46Speaker 1: mean those native browser alert boxes that drop down from
- 20:50Speaker 1: the top of the screen asking are you sure you
- 20:53Speaker 1: want to leave this page? Oh?
- 20:54Speaker 2: The ones that freeze everything.
- 20:56Speaker 1: Yeah, they freeze the entire browser window until.
- 20:59Speaker 2: You deal with them the browser, and they will completely
- 21:01Speaker 2: freeze your script too. Because those alerts are generated by
- 21:04Speaker 2: the browser itself or even the operating system, they are
- 21:07Speaker 2: not part of the HTML dom. Wait really, yeah, you
- 21:10Speaker 2: cannot inspect them. You cannot write an XPath for the
- 21:13Speaker 2: ok button on a native alert if they.
- 21:15Speaker 1: Aren't in the dom. How on earth does Selenium interact
- 21:18Speaker 1: with them.
- 21:18Speaker 2: Well, the driver has a dedicated interface just for handling
- 21:21Speaker 2: these native interruptions. When an alert fires, you instruct the
- 21:26Speaker 2: code to switch its focus to the alert manager. You
- 21:29Speaker 2: basically grab the currently active alert object into your code's memory.
- 21:33Speaker 1: So you capture the alert itself as a programmable.
- 21:35Speaker 2: Object precisely, and once captured, the interface exposes specific methods
- 21:40Speaker 2: for it. You can call dot accept, which tells the
- 21:43Speaker 2: browser to simulate clicking okay nice. You can call dot dismiss,
- 21:47Speaker 2: which simulates clicking cancel. You can even use dot text
- 21:51Speaker 2: to extract the message inside the alert box, just to
- 21:54Speaker 2: verify the servers and the right air code before dismissing it.
- 21:57Speaker 1: This has been such a fascinating breakdown of how Autumn
- 22:00Speaker 1: actually functions under the hood. We started with just a
- 22:03Speaker 1: blank maximize browser window. We learn how the dom tree works,
- 22:07Speaker 1: how to navigate it using IDs and architectural xpaths.
- 22:10Speaker 2: It's a lot to take in, it really is.
- 22:12Speaker 1: We explore the necessity of clearing text before typing, using
- 22:16Speaker 1: explicit weights to outsmart network latency, looping through hidden arrays
- 22:20Speaker 1: and drop downs, and context switching into secure eyeframes.
- 22:23Speaker 2: It really demystifies the magic of automation, doesn't it. We
- 22:27Speaker 2: take the intuitive, visual, human experience of browsing the web
- 22:30Speaker 2: and we strip it down to raw structure and precise
- 22:33Speaker 2: programmatic logic.
- 22:34Speaker 1: We really do. To wrap things up, I actually have
- 22:37Speaker 1: a quick mental exercise for you listening to try later today.
- 22:41Speaker 1: Next time you are on your favorite web application, maybe
- 22:45Speaker 1: checking your bank balance or reading the news, right click
- 22:47Speaker 1: a button on the page, hit inspect and just look
- 22:50Speaker 1: at the raw HTML that pops up.
- 22:52Speaker 2: That's a great exercise.
- 22:53Speaker 1: Yeah, see if you can identify its tag name, its
- 22:55Speaker 1: glasses or an ID. Look at how deeply it is
- 22:58Speaker 1: nested inside other containers. Try to visualize how you would
- 23:02Speaker 1: write the directions for a script to find it. It
- 23:05Speaker 1: permanently changes the way you experience the Internet.
- 23:08Speaker 2: It absolutely does. You start seeing the matrix behind the design.
- 23:11Speaker 1: You really do. And as you're looking at that raw code,
- 23:13Speaker 1: I want to leave you with one final thought. Now
- 23:16Speaker 1: that you know how methodically a script parses a web page,
- 23:20Speaker 1: you know, bypassing the visual design entirely to read the
- 23:23Speaker 1: underlying data structures, hidden rays, and secure frames, how does
- 23:27Speaker 1: that change your perspective on the Internet's traffic.
- 23:30Speaker 2: That's a deep question, right.
- 23:32Speaker 1: If a simple automation script can read and extract data
- 23:35Speaker 1: from the matrix of a web page this easily, what
- 23:38Speaker 1: does that imply about the massive fleets of invisible bots
- 23:40Speaker 1: that are constantly crawling, scraping, interacting with the Internet right
- 23:44Speaker 1: alongside us, every single second of the day. Something to
- 23:47Speaker 1: think about.