How can I use the find_elements_by_xpath?

598    Asked by Ankityadav in Cyber Security , Asked on Feb 24, 2022

 How should I use driver.find_elements_by_xpath() to get all the data-row elements? 

I do not need the other tr tags. In my current code I just check all the tr tags and then throw out the ones that raise a NoSuchElementException but that gets very slow since my implicit_wait() is set to 15 seconds and the table is about 100 rows long, see below for code snippet.


table_element = t_find_element_by_id(driver, "table_name")
body_element = t_find_element_by_tag_name(table_element, "tbody")
row_elements = t_find_elements_by_tag_name(body_element, "tr")
for item in row_elements:
    column_elements = t_find_elements_by_tag_name(item, "td")
    try:
        a_tag_element = t_find_element_by_tag_name(columns[0], 'a')
    except NoSuchElementException:
        continue


Answered by ananya Pawar

Try using the below for find_elements_by_xpath.


//table[@id='table_name']//tbody//tr[not(@class)]
This will give you the highlighted tr tags.
Java Example:
List elements = driver.findElements(By.xpath("//table[@id='table_name']//tbody//tr[not(@class)]"));
for (WebElement ele : elements) {
System.out.println(ele.getText());
                        }


Your Answer

Interviews

Parent Categories