Selenium Automating Web Browsers

Selenium is used to automate boring tasks; it automates browsers. From navigating the web to automatically logging into accounts to creating bots for various tasks can be achieved using Selenium.

First of all, let’s install the dependencies:

pip install selenium

pip install webdriver-manager

Navigating the web

We import the dependencies:

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

We then initialize the webdriver:

driver = webdriver.Chrome(ChromeDriverManager().install())

In the first instance, let’s try to navigate a web page using the get() method. The get() method will open up the webpage or the url that was inputted; please note that the full link must be provided for this to work.

driver.get("https://duckduckgo.com/")

Suppose you wanted to maximize the window using the maximize_window() method and not have python run through the rest of the code; you’d then use the implicitly_wait() method to pause.

driver.maximize_window()

driver.implicitly_wait(4)

If you’d like information on the website, you can use the title method to get the name or the title of the website, the current_url method to get the url, and the page_source to get the html code of the page.

print(driver.title)

print(driver.current_url)

print(driver.page_source)

To input a piece of text into the search bar, we must first try to identify the search bar using the “inspect” button (right-click –> inspect).

For duckduckgo.com, there’s an id available, but you can also get other attributes. The next method we use is the find_element_by_id() method. This method’s purpose is to select the element of concern.

search_bar = driver.find_element_by_id("search_form_input_homepage")

However, you can replace this with any of the other attributes. For instance, if you have the name available, then use the find_elements_by_name() method. We did not have to use the id; we could have used something else altogether had we wanted. For instance, we could have used the name as follows:

search_bar = driver.find_element_by_name("q")

Since we have located the search bar, we can now input a piece of text using the send_keys() method.

search_bar.send_keys("SeleniumHQ")

Theoretically, what we’d do is to click on the Next button. So what do we do? Yes, you’ve guessed it! We method the elements and find the id or another attribute of the next button. Then, we use the click() method to click on the button we have selected using an attribute (such as id).

button = driver.find_element_by_id("search_button_homepage")

button.click()

At this point, you get a list of websites; it’s much like typing SeleniumHQ into the search bar of the duckduckgo.com site and pressing the next button. Now, let’s try to click on the first page we get. Here, I’m going to use xpath, but you can use any of the attributes. Please note that when using xpath, the quotation marks inside are single and outside are double (“//*[@id=’r1-0′]/div/h2/a[1]”).

page = driver.find_element_by_xpath("//*[@id='r1-0']/div/h2/a[1]")

page.click()

Logging into an Email Account

Now, let’s suppose we want to log into an email account. As always, we began importing the necessary modules and initializing them as we did to navigate a web page. Here, we’ll import time as well.

from selenium import webdriver

from webdriver_manager.chrome import ChromeDriverManager

import time

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.gmail.com/")

driver.maximize_window()

driver.implicitly_wait(4)

user = driver.find_element_by_xpath("//*[@id='identifierId']")

user.send_keys("vy814683@gmail.com")

next = driver.find_element_by_xpath("//*[@id='identifierNext']/div/button/div[2]")

next.click()

Next, we are going to use the time.sleep() method to prevent synchronization errors (at times, it takes longer to load a page, and while it loads, the rest of the script is executed and an error reported). In an attempt to prevent synchronization errors, we’ll give it time to load everything.

time.sleep(3)

password = driver.find_element_by_xpath("//*[@id='password']/div[1]/div/div[1]/input")

password.send_keys("Password1.")

next2 = driver.find_element_by_xpath("//*[@id='passwordNext']/div/button/div[2]")

next2.click()

Sending Emails using Selenium

To send emails, we need two parts: how to login into Gmail from the previous section and how to send emails from this section. Sending an email is as easy as navigating the web or even logging into Gmail. Once again, we choose the find_element_by_xpath() method or one that is similar such as find_element_by_id() or yet another to find each component of the message to be sent – the compose button, the destination input bar, the subject input bar, the body, and the send button. We must locate them, populate them with text when needed and click on buttons. Simple enough. So let’s try it:

First, let’s find the compose button using xpath, and then click on it:

time.sleep(1)

compose_button = driver.find_element_by_xpath("//*[@id=':2p']/div/div")

compose_button.click()

Next, let’s add a destination address in the destination address input bar:

time.sleep(2)

to_input_bar = driver.find_element_by_id(':8c')

to_input_bar.send_keys("vy814683@gmail.com")

Now we add a subject, a body and click on the send button:

subject = driver.find_element_by_id(':7u')

subject.send_keys("Test Email")

body = driver.find_element_by_xpath("//*[@id=':8z']")

body.send_keys("This is an automated message sent using Selenium.")

send_button = driver.find_element_by_xpath("//*[@id=':7k']")

send_button.click()

Ok, so, making a simple program to log into and send mails is now too easy? We don’t have to stop there; we can create all kinds of bots.

Mouse Actions

The next thing to learn is to deal with mouse actions – from hovering to dragging and dropping files.

We must first specify the tabs; in this case, I will specify three tabs – the home tab, the electronics tab, and the cell phone tab (cell phone is nested within electronics). Ok, so for this example, I chose ebay.ca. On ebay.ca, there are many tabs – one for home and one for electronics. We typically start on the home tab. Here, let’s say I would like to navigate the Electronics tab and then the cell phone and accessories tab within it and click it. We could use mouse actions to carry out the following.

We begin by importing the necessary and opening ebay.ca.

From selenium import webdriver

from selenium.webdriver import ActionChains

from webdriver_manager.chrome import ChromeDriverManager

import time

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://www.ebay.ca/")

Next, we need to add the elements we are concerned about. In this case, I have the three red boxes in the previous picture.

home = driver.find_element_by_xpath("//*[@id='mainContent']/div[1]/ul/li[1]/span")

electronics = driver.find_element_by_xpath("//*[@id='mainContent']/div[1]/ul/li[5]/a")

cell_phone = driver.find_element_by_xpath("//*[@id='mainContent']/div[1]/ul/li[5]/div[2]/div[1]/nav[1]/ul/li[1]/a")

We then initialize the ActionChains and use the move_to_element() method to move from home to electronics and cell phones. Once at the cell phone and accessories tab, we click on it. For all of this to work, we must add the perform() method at the end, or nothing will happen.

actions = ActionChains(driver)

next_step = actions.move_to_element(home).move_to_element(electronics)

next_step.move_to_element(cell_phone).click()

next_step.perform()

However, you can perform various actions with the mouse – from right-clicking to drag and drops. Let’s get an example where you can right-click. To right-click, you need the context_click() method; pass within it that which you wish to click. In this case, we wish to first right click on the button, so we say driver.context_click(button) – right-click the button. Then when the right-clicking shows us a selection menu with options, we ask it to click on one of the options displayed – click(copy), which will click on the Copy tab.

from selenium import webdriver

from selenium.webdriver import ActionChains

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://ift.tt/3fq1iRI")

button = driver.find_element_by_xpath("/html/body/div/section/div/div/div/p/span[1]")

copy = driver.find_element_by_xpath("/html/body/ul[1]/li[3]")

actions = ActionChains(driver)

actions.context_click(button).click(copy)

actions.perform()

You can do so many things with selenium, but we will also take a look at drag and drop. There are two ways of dragging and dropping an item into its target destination. One is using the drag_and_drop() method, and the other is using a more complicated technique:

from selenium import webdriver

from selenium.webdriver import ActionChains

from webdriver_manager.chrome import ChromeDriverManager

import time

driver = webdriver.Chrome(ChromeDriverManager().install())

driver.get("https://ift.tt/2SPwpOO")

driver.implicitly_wait(1)

driver.maximize_window()

# Select source image on page to drag

source = driver.find_element_by_id("node2")

# Select the target where you want it dropped

target = driver.find_element_by_xpath("//*[@id='box1']")

time.sleep(5)

# Initialize the actionchains

action_chains = ActionChains(driver)

# Move the source to the target

action = action_chains.click_and_hold(source)\

    .move_to_element(target)\

    .release(target)\

    .click(target)\

    .perform()

Selenium is a very powerful tool to do very many things. In this tutorial, we covered a few of the things that selenium can do; however, once you familiarize yourself with it, you can create all sorts of bots to automate the browser.

Happy Coding!



from Linux Hint https://ift.tt/3wbTDNA

Post a Comment

0 Comments