How To: Table Data In Selenium (2 Min) Using Python, PyTest & PyCharm

preview_player
Показать описание
In this tutorial, you'll learn how to extract table data in Selenium using Python, PyTest and PyCharm.



Video Transcript:

Hi guys, this is Abhi from Gokce DB. In this video, you are going to learn how to extract data from a web table in Selenium using Python.

Let’s start by looking at the past scenario on Mozilla dot org. I want to go to this specific URL and read the items sold in the August two thousand sixteen table.

More specifically, I want to assert whether the total item sold in Amsterdam is equal to three fifteen or not. Now, let’s look at the past directory.

I have a contest dot py file where I’m defining the driver fixture function to initialize the Chrome web driver. In the past file, I’m passing the driver fixture function as the first argument to my past function.

On line five, I’m using the driver dot get method to go to the Mozilla dot org URL. On line seven, I’m reading the text of the sixth row of the item sold on the table.

Line twelve, I’m splitting the table row with this space delimiter and looping through the individual item. Within the four loops, if the item is after type int I add it to the total variable.

Else, I ignore the item. Finally, on line nineteen, I’m asserting whether the total is equal to three fifteen or not.

Now, let’s run this program to see what the output looks like. As you can see, since the total came out to three hundred fifteen our task passed.

Let’s see what happens if I change the total to three hundred twenty and re-run the program. This time our task failed as expected.

Finally, to export the past results in an HTML format click on the EXPORT TEST RESULTS button. There you have it.

Make sure you like, subscribe, and turn on the notification bell. Until next time.

def test_table_row_sum(driver):

By.XPATH, '// *[ @ id = "content"] / article / div[7] / div[1] / table / tbody / tr[6]').text

print("Table row text:", table_row)
total = 0
try:
total += int(item)
except ValueError:
print("Ignoring item", item, "because it's not an int")

print("Sum:", total)
assert total == 315

import pytest
from selenium import webdriver
import os

def driver():
driver = webdriver.Chrome(executable_path=root_dir + '/resources/chromedriver')
# Time in seconds driver should wait when searching for an element if it is not
# immediately present
yield driver
Рекомендации по теме