How do we do this?
Python selenium WebDriver provides an in-built method
What are we doing here ?
userName = driver.find_element_by_xpath("//button[@name='username']") driver.execute_script("arguments.click();", userName)
Step 1: Inspect and capture the element using webdriver provided methods like ‘find_element_by_xpath’
userName = driver.find_element_by_xpath(“//button[@name=’username’]”)
Another more frequently faced issue is scrolling to the bottom of the web page and you can perform this operation in a single line of code
userName = driver.find_element_by_xpath("//button[@name='username']") password = driver.find_element_by_xpath("//button[@name='password']") driver.execute_script("arguments.click();arguments.click();", userName, password)
driver.execute_script(“arguments.click();arguments.click();”, userName, password)
How to return values?
print driver.execute_script('return document.getElementById("fsr").innerText')
What happens when element not found?
Scenario 1: Trying to read property using ‘print driver.execute_script(‘return document.getElementById(“fsr”).innerText’)‘ but no such element available in web page. We get following message in exception trace.
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property ‘innerText’ of null
selenium.common.exceptions.WebDriverException: Message: unknown error: document.getElementById(…).clic is not a function
- get an elements text or attribute
- find an element
- do some operation on element like click()
- change attributes of an element
- scroll to an element or location of a web page
- wait until the page is loaded
Please let us know your thoughts in comments. Happy coding!!!