How python automates testing selenium executes js scripts
Editor to share with you how python automates testing selenium and executes js scripts. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.
WebDriver has two methods to execute JavaScript scripts.
(1) synchronous execution: execute_script
(2) Asynchronous execution: execute_async_script
From selenium import webdriverfrom time import sleepclass TestScriptss (object): def setup (self): self.driver = webdriver.Chrome () self.driver.get (http://www.baidu.com) # execute js prompt box And click def test_scripts (self): self.driver.execute_script ("alert ('test')") sleep (2) self.driver.switch_to.alert.accept () self.driver.quit () # execute the script to get the current page title def test_scripts (self): js = "return document.title" title = self.driver.execute _ script (js) print (title) sleep (2) self.driver.quit () # format text box def test_format (self): js = 'var Q = document.getElementById ("kw") Q.style.border = "10px solid red" 'self.driver.execute_script (js) sleep (5) self.driver.quit () # realize page scrolling through script def test_scroll (self): self.driver.find_element_by_id (' kw'). Send_keys ('selenium test') self.driver.find_element_by_id (' su') .click () sleep (2) # search results page scroll js = 'window.scrollTo (0) Document.body.scrollHeight)'# synchronous execution self.driver.execute_script (js) # Asynchronous execution # self.driver.execute_async_script (js) sleep (2) self.driver.quit () these are all the contents of the article "how python automates testing selenium executes js scripts" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!