Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting text of a table quickly in Selenium

I'm trying to parse a number of columns in a table into a dictionary using Selenium, but what I have seems slow. I'm using python, Selenium 2.0, and webdriver.Chrome()

table = self.driver.find_element_by_id("thetable")
    # now get all the TR elements from the table
    all_rows = table.find_elements_by_tag_name("tr")

    # and iterate over them, getting the cells
    for row in all_rows:
        cells = row.find_elements_by_tag_name("td")
        # slowwwwwwwwwwwwww
        dict_value = {'0th': cells[0].text,
                 '1st': cells[1].text,
                 '2nd': cells[2].text,
                 '3rd': cells[3].text,
                 '6th': cells[6].text,
                 '7th': cells[7].text,
                 '10th': cells[10].text}

The problem seems to be getting the 'text' attribute of each td element. Is there a faster way?

like image 336
PearSquirrel Avatar asked Dec 21 '14 18:12

PearSquirrel


People also ask

How do I grab text in Selenium?

We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).

How do you iterate through a table in Selenium?

Code Explanation: Using chrome driver we locate the web table and get total number of row using XPath “.//*[@id='leftcontainer']/table/tbody/tr/td[1]” Using for loop, we iterate through total number of rows and fetch values one by one. To get next row we use (i+1) in XPath.

How do I find the XPath of a row in a table?

Let's select an element in the web table and find its XPath. For Chrome, right-click and inspect the given element to find its XPath. To find the XPath of a UI element in Firefox, right-click on the desired element, go to “Inspect Element” to open the inspector which will help identify its XPath.


1 Answers

Alternative option.

If later (after the loop), you don't need interactiveness that selenium provides you with - you can pass the current HTML source code of the page to lxml.html, which is known for it's speed. Example:

import lxml.html

root = lxml.html.fromstring(driver.page_source)
for row in root.xpath('.//table[@id="thetable"]//tr'):
    cells = row.xpath('.//td/text()')
    dict_value = {'0th': cells[0],
                  '1st': cells[1],
                  '2nd': cells[2],
                  '3rd': cells[3],
                  '6th': cells[6],
                  '7th': cells[7],
                  '10th': cells[10]}
like image 137
alecxe Avatar answered Nov 06 '22 03:11

alecxe