Issue
I'm using selenium to automate a task on a very dynamic website with pyhton. For this reason certain HTML elements of the current loaded page may or may not be present at the moment of the request from my code. How exactly the webdriver instance gets updated and receives the new data from the web page?
Is it constantly connected and receive the change in the HTML code instantly?
Or it first download a first verion of the page when driver.get()
is called, and then updates it whenever a function such as .find_element_by_class_name()
is called?
Solution
Q. Is it constantly connected and receives the change in the HTML code instantly?
Ans. Well, for each Selenium command
, there will be an HTTP request
that will send to the browser driver
and then to the server and the command will get, A HTTP response will be returned and the command/commands will get executed based on the response.
Now let's say in a situation you do,
driver.get()
Basically, this is a selenium command.
It would fire an HTTP request
stating to launch the URL provided
. Based on the response (200-Ok or anything else)
, you would either see the WebPage getting loaded or an error message.
Same way in Sequence all the Selenium commands will get executed.
It's obvious that we need locators to locate web elements in the UI.
Once we have them, we can tightly couple them with
driver.find_element_by_***
methods.
Things to be noted down here
You need to learn/understand :
- Implicit Waits.
- Explicit Waits.
- Fluent Waits.
Implicit Waits :
By implicitly waiting, WebDriver polls the DOM for a certain duration
when trying to find any element. This can be useful when certain elements on the webpage are not available immediately and need some time to load.
Basically what it means is, whenever you use drive.find_element
it gonna look for implicit waits if you have defined one.
If you've not defined one implicit wait, then the default value is 0.
Explicit wait
They allow your code to halt program execution
, or freeze the thread
, until the condition you pass it resolves. The condition is called with a certain frequency until the timeout of the wait is elapsed. This means that for as long as the condition returns a falsy value
, it will keep trying and waiting.
FluentWait
FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
Reference Link
Updated :
PS : Please check in the dev tools
(Google chrome) if we have unique entry in HTML DOM
or not.
Steps to check:
Press F12 in Chrome
-> go to element
section -> do a CTRL + F
-> then paste the xpath
and see, if your desired element
is getting highlighted with 1/1
matching node.
Locators (by priority from top):
- ID
- name
- classname
- linkText
- partialLinkText
- tagName
- css selector
- xpath
Answered By - cruisepandey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.