Executing JavaScript Snippets in Selenium Using Python
In your provided code snippet, you encountered difficulty executing a JavaScript snippet within the context of your Selenium script. Initially, you attempted to use the syntax "selenium.GetEval", but this resulted in an AttributeError.
To resolve this, you should use the "browser.execute_script" method instead. This method allows you to evaluate arbitrary JavaScript code within the DOM of the current page.
Here's a modified version of your code that includes the "browser.execute_script" method:
<code class="python">from selenium import webdriver import selenium from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.common.keys import Keys import time patch = raw_input("Enter patch number\n") rel = raw_input("Enter release\n") plat = raw_input("Enter port\n") browser = webdriver.Firefox() browser.get("xxxxxxxxxxxxxxxxx") pdtfamily = browser.find_element_by_id("prodFamilyID") pdtfamily.send_keys("Database & Tools" + Keys.TAB) time.sleep(5) pdt = browser.find_element_by_id("productID") pdt.send_keys("Intelligent Agent" + Keys.TAB) time.sleep(5) pdt1 = browser.find_element_by_id("patchCacheChkBxID") pdt1.send_keys(Keys.SPACE) time.sleep(5) pdt7 = browser.find_element_by_id("M__Idf") pdt7.send_keys(plat) pdt8 = browser.find_element_by_id("M__Idg") pdt8.send_keys("American English") # Execute JavaScript snippet using browser.execute_script browser.execute_script("submitForm('patchCacheAdd',1,{'event':'ok'});return false") browser.close()</code>
By using the "browser.execute_script" method, you can now execute the desired JavaScript snippet within your Selenium script.
The above is the detailed content of How can I Execute JavaScript Snippets in Selenium Using Python?. For more information, please follow other related articles on the PHP Chinese website!