Scrolldown a table inside a div tag in through Selenium in Python

Viewed 540

I wanted to scroll till the last row of https://covid19.who.int/table using Selenium framework in python.

See below my snippet

url = 'https://covid19.who.int/table/'
Path_ChromeDriver = 'E:\chromedriver_win32\chromedriver.exe'
driver = webdriver.Chrome(Path_ChromeDriver)
driver.get(url)
time.sleep(10)
driver.execute_script("window.scrollTo(0,document.body.scrollHeight)")
target = driver.find_element_by_xpath('//div[@role="rowgroup"]//div[@role="row"][last()]//div[@class="column_Deaths td"]/div')
driver.execute_script("arguments[0].scrollIntoView(true);", target)

But this doesn't help me to scroll down the div tag. Any help will be highly appreciated. I am looking to extract all the data from the table starting from the “Americas” row.

See screenshot for data I need.enter image description here Thanks in advance

1 Answers

First get all the rows of the table using this line of code.

rows = driver.find_elements_by_css_selector("div[class=\"tr depth_0 \"]")

Now scroll to last row like this:

driver.execute_script("arguments[0].scrollIntoView()",rows[len(rows)-1]

By this, the table will be scrolled till the last row which will be in rows variable.

So, if you want to scroll to the end of the table, you will have to use a while loop and get all rows again and scrollIntoView the last row. While loop because I checked that finding all rows will only return 20-28 rows which are visible on the page. So, use a while loop to go till the end.

Related