'text based HTML of the element selenium python

I am actually trying to scrap a website. In fact I have a table like this below :

Table

I would like to navigate to the line that contains the word "Maître", but I am not able to find this line with selenium. Below the html code of the page :

Html code 1 Html code 2

And this is my code :

objets_de_risque = driver.find_element(By.ID,'sidebar-link-0-1')
driver.execute_script("arguments[0].click();", objets_de_risque)
code_h = driver.find_element(By.ID,'input-search-1')
code_h.send_keys(Keys.CONTROL + "a")
code_h.send_keys(Keys.DELETE)
code_h.send_keys("H1404")
code_h.send_keys(Keys.ENTER)
driver.switch_to_frame("main1")
line_maitre = driver.find_elements_by_xpath("//*[contains(text(), 'Maître')]")
driver.execute_script("arguments[0].click();", line_maitre)

The last 2 lines doesn't seem to work. Is there a way to go to this line with selenium ?

Thank you very much



Solution 1:[1]

find_elements() returns a list. Instead you need find_element() to locate the element.


Solution

To locate a visible element you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies:

  • Using XPATH:

    line_maitre = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//td[text()=''Maître'']")))
    

However to click on it as per your code in a single line:

driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td[text()=''Maître'']"))))

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 undetected Selenium