Selenium and LINK_TEXT

1.5k Views Asked by At

I tried to scrapp a cost with a LINK TEXT but my scrapping method can't find the TEXT:

budget = budgets.append((driver.find_element(By.LINK_TEXT, 'Total budget/expenditure:')).text)

The web site is : https://keep.eu/projects/12880/A-la-d-couverte-des-plus-be-EN/

It works with the XPATH but i need to scrap many page like this one and sometimes the To total budget/expenditure and European Union Funding was not exactly at the same place.

The error is that :

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Total budget/expenditure:"}
  (Session info: chrome=100.0.4896.127)

I don't know why sometimes I can used the LINK-TEXT and sometimes no. I tried PARTIAL_LINK_TEXT too but it can't works.

1

There are 1 best solutions below

3
On BEST ANSWER

Link Text

A linkText is used to identify the hyperlinks on a web page. It can be determined with the help of an anchor tag <a>.

But your desired element is within a <strong> tag, so By.LINK_TEXT won't work here.

<p>
    <strong>Total budget/expenditure: </strong>
    " EUR 313 300.00"
</p>

Solution

To locate the element you can use either of the following locator strategies:

  • Using xpath and contains():

    element = driver.find_element(By.XPATH, "//p//strong[contains(., 'Total budget/expenditure:')]")
    
  • Using xpath and starts-with():

    element = driver.find_element(By.XPATH, "//p//strong[starts-with(., 'Total budget/expenditure:')]")