1

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

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Actually, I would like to double click on it not just a single click. I read about ActionChains. I wrote this line actionChains = ActionChains(line_maitre) but I get an error message telling me 'WebElement' object has no attribute 'w3c'
Sounds to be a different question all together. Feel free to raise a new question as per your new requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.