In
this instructional exercise, we should break down and see distinctive
approaches to execute Javascript explanations/directions through Python
Selenium WebDriver. It might so occur in some constant undertakings, Selenium
WebDriver can't play out some activity on a specific web component and
connecting with web components through Javascript was the main conceivable
arrangement.
For
instance, since WebDriver recreates end-client association, it is regular that
it will decline to tap on a component that isn't unmistakable to end client.
There can be a few other comparative reasons or situations. In these cases, can
depend on Selenium
Training in Bangalore JavaScript to click or play out a few activities on
that web component and this javascript articulation can be executed through
WebDriver.
You
can do everything that the WebElement interface does and more with Javascript.
What is JavaScript?
JavaScript
is a scripting dialect that keeps running on customer side, i.e on the program
and does some enchantment things when you surf through site pages.
How would we do this?
Python
selenium WebDriver gives an in-constructed technique
driver.execute_script("some
javascript code here");
There
are two different ways through which we can execute JavaScript inside the
program.
Executing JavaScript at record root
level:
For
this situation we catch the component, that we need to work with, utilizing
javascript gave strategies, at that point pronounce a few activities on it and
execute this javascript utilizing WebDriver.
Precedent:
javaScript
= "document.getElementsByName('username')[0].click();"
driver.execute_script(javaScript)
What are we doing here?
·
We are examining and bringing the
component by one of its property 'name' (Also id and class properties can be
utilized) utilizing javascript.
·
Declare perform click activity on a
component utilizing javascript.
·
Call execute_script() technique and
pass the javascript that we made as String esteem
Notice
[0] in getElementsByName('username')[0] proclamation. JavaScript capacities
getElementsByName, getElementsByClassName and so on restore every single
coordinating component as a cluster and for our situation we have to follow up
on first coordinating component which can be gotten to through record [0]. On
the off chance that you comprehend what you are doing i.e. on the off chance
that you know list of component that you need to work, you can
straightforwardly utilize the list like getElementsByName('username')[2]
While
on the off chance that you are utilizing JavaScript work 'getElementById', you
no compelling reason to utilize any ordering as it will return just a single
component.
While
on execution, WebDriver will infuse the JavaScript explanation into the program
and the content will play out the activity; in our model perform click task on
the objective component. This javascript has its very own namespace and does
not meddle with javascript in real website page.
Executing
JavaScript at component level:
For
this situation we catch the component that we need to work with utilizing
webdriver, at that point we proclaim a few activities on it utilizing
javascript and execute this javascript utilizing WebDriver by passing web
component as a contention to the javascript. Is it confounding? Disappoints
break
Precedent:
userName
= driver.find_element_by_xpath("//button[@name='username']")
driver.execute_script("arguments[0].click();",
userName)
Stage
1: Inspect and catch the component utilizing webdriver gave strategies like
'find_element_by_xpath'
userName
= driver.find_element_by_xpath("//button[@name='username']")
Stage
2: Declare perform click activity on the component utilizing javascript.
arguments[0].click()
Stage
3: Call execute_script() technique with the javascript explanation that we made
as String worth and webelement caught utilizing webdriver as contentions
driver.execute_script("arguments[0].click();",
userName)
The
over two lines of code can be abbreviated to beneath configuration where we
discover component utilizing webdriver, proclaim some javascript capacities selenium
training institutes in Marathahalli and execute the javascript
utilizing webdriver.
driver.execute_script("arguments[0].click();",
driver.find_element_by_xpath("//button[@name='username']"))
Another
all the more every now and again confronted issue is looking to the base of the
website page and you can play out this task in a solitary line of code
driver.execute_script("window.scrollTo(0,
document.body.scrollHeight);")
How to return esteems?
Other
vital part of javascript agent is, it very well may be utilized to get esteems
from web component; That implies execute_script() strategy can return esteems.
For
instance:
print
driver.execute_script('return
document.getElementById("fsr").innerText')
Note
that on the off chance that you need something returned by javascript code, you
have to utilize return. Likewise components can be situated with selenium and
go into the content.
No comments:
Post a Comment