123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Usage Of Javascript With Python Selenium Testing

By Author: Siyaram ray
Total Articles: 396

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.

More About the Author

best institute for selenium training in Bangalore – Infocampus. Infocampus is an excellence training center for selenium courses in Bangalore. At Infocampus, selenium training is focused on practical and real time examples. Contact Infocampus at 8884166608 or visit http://infocampus.co.in/best-selenium-testing-training-center-in-bangalore.html for complete details on selenium training.

Total Views: 37Word Count: 630See All articles From Author

Computer Programming Articles

1. Why Take The Services Of An Iphone Application Development Company
Author: Moon Technolabs

2. Best Cpanel Providers That Offers Whm Cpanel License At Reasonable Prices
Author: Buy cPanel

3. 6 New Technologies Used For Java Developer
Author: infocampus

4. Top Java Web Systems To Use In 2019
Author: infocampus

5. A Brief Look At Custom Healthcare Software Development & Its Future.
Author: pawan yadav

6. Know About Importance Of Java Programming Language
Author: APTRON Noida

7. The Complete Guide To Understanding The Enterprise Application Integration
Author: rishabh Shukla

8. What Java Programmers Should Learn In 2019
Author: infocampus

9. Spring Framework Bean Scopes
Author: Kapil Gupta

10. Master The Art Of Java Se Support Roadmap With These Tips
Author: infocampus

11. How To Use Magento Import Export Extensions To Migrate Product Data?
Author: Shailesh Gajjar

12. A Complete Guide For Advanced Java
Author: infocampus

13. What Rings A Bell When You Hear The Word Java?
Author: infocampus

14. Most Recent Java Technologies And Trends
Author: infocampus

15. How To Disable Firewall On Mac
Author: karen minton

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: