It may so happen that in some real-time projects, Selenium WebDriver cannot perform an action on a particular web element. For example, since WebDriver simulates end-user interaction, it is natural that it will refuse to click on an element that is not visible to the end user (sometimes it also happens even though the web element is visible on the page). There can be several other similar reasons or scenarios.
Python Selenium WebDriver provides a built-in method:
 in the
getElementsByClassName , and so on return all matching elements as an array. In our case, we need to act on the first matching element that can be accessed via
index  . If you know what you are doing, i.e., if you know the index of the element you want to operate, you can directly use the index, such as
getElementById ', you do not need to use any indexing, as it will return only one element (‘id’ should be unique).
Is this confusing? Let's break it down.
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 '
userName = driver.find_element_by_xpath("//button[@name='username']")
Step 3: Call
Another issue faced more frequently is the need to scroll to the bottom of the web page. 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)
In this case, usage of the order of web elements matters. Accessing
driver.execute_script("arguments.click();arguments.click();", userName, password)
execute_script() method can return values.
print driver.execute_script('return document.getElementById("fsr").innerText')
Scenario 1: We're trying to read a property using '
print driver.execute_script('return document.getElementById("fsr").innerText') ' but there is no such element available in the web page. We get the following message in the exception trace:
selenium.common.exceptions.WebDriverException: Message: unknown error: Cannot read property 'innerText' of null
print driver.execute_script('document.getElementById("fsr").clic();') '. (Note the spelling mistake in the
click() method name.)
selenium.common.exceptions.WebDriverException: Message: unknown error: document.getElementById(...).clic is not a function
Are you looking for experienced, reliable, and qualified Python developers? If yes, you have reached the right place. At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, our full-stack Python development services...
Looking to build robust, scalable, and dynamic responsive websites and applications in Python? At **[HourlyDeveloper.io](https://hourlydeveloper.io/ "HourlyDeveloper.io")**, we constantly endeavor to give you exactly what you need. If you need to...
In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.
Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...
After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...