-1

I am trying to click at a href tag text, in this case "São Vicente":

<li>
<a href="/comprar-casas/braga/sao-vicente/mapa">São Vicente</a>
<span class="subdued">139</span>
</li>

Using the code:

zone = "São Vicente"

try: 
        tiredAF = page.locator('a:has-text("' + zone + '")')
        tiredAF.click()

But playwright doesn't find anything.

Any idea on what is going wrong?

//Update

After debuggin the case, I could find out the follow message error is displayed:

=========================== logs ===========================
waiting for get_by_text("Vicente").first
  locator resolved to <a href="/comprar-casas/braga/sao-vicente/mapa">São Vicente</a>
attempting click action
  waiting for element to be visible, enabled and stable
    element is not visible - waiting...
============================================================

I did in fact insert a time.sleep(10) to make sure everythin loads in the page, but it seems that was not enough.

Source page is https://www.idealista.pt/comprar-casas/braga/braga/braga/mapa

  • It runs when I add string literals to the string: `zone = 'São Vicente'` – candre Jul 08 '23 at 23:30
  • @candre the original code has " in it, I forgot to type, I edited the code to reflect that – gustavo matteo Jul 09 '23 at 00:33
  • does it work if you put the zone name directly into the selector instead of doing string concatenation? What if you just use `"Vicente"` on its own? Without knowing the source page, is it possible the target link has not been loaded into the DOM yet at the moment you are trying to select and click it? – nigh_anxiety Jul 09 '23 at 02:03
  • Second thought - you may need to use an HTML escape. `zone = "São Vicente"`. It looks like the built-in `html.escape()` function doesn't escape accented characters like this. Currently looking to see if any built-in function automatically handles those characters or not. – nigh_anxiety Jul 09 '23 at 02:15
  • Strangely enough, neither `page.locator('href:has-text("Vicente")')` `page.locator('href:has-text("São Vicente")')` `page.locator('a:has-text("Vicente")')` or `page.locator('href:has-text("São Vicente")')` worked, I even added a time.sleep(10) to guarantee everything would load in time. `zone = "São Vicente"` did not do the trick too – gustavo matteo Jul 09 '23 at 17:53
  • While debugging the situation, I found out that somehow the element is not visible at the moment we try to click it. I could not figure out why the element is not visible because it is in fact visible and clickable by human eye. I will add the error and source page to the question to show what is happening @nigh_anxiety – gustavo matteo Jul 09 '23 at 18:05
  • Not the ideal, but I made it work by using `page.get_by_role("link", name=zone, exact=True).click()` – gustavo matteo Jul 09 '23 at 18:18

1 Answers1

1

Looking at the page source, there is more than one element that matches your selector, and the first one found is in a collapsed drop-down menu.

In order from the top: At line 324 - this is the drop down menu accessible from the "Braga ▼" at the top of the page

<li class="breadcrumb-dropdown-subitem-element-list" data-location-id="0-EU-PT-03-03-001-49">
<a href="/comprar-casas/braga/sao-vicente/mapa">São Vicente</a>
<span class="breadcrumb-navigation-sidenote">139</span>
</li>

At line 378 - this is the one displayed on screen that you're trying to click):

<li>
<a href="/comprar-casas/braga/sao-vicente/mapa">São Vicente</a>
<span class="subdued">139</span>
</li>

At line 566, as part of a <script type="text/microtemplate"> block which appears to define the list of links appearing earlier in the page.

<a href="/comprar-casas/braga/sao-vicente/mapa"><span class="sublocationText">São Vicente</span></a>
<span class="subdued">139</span>

Your initial attempt is finding that the element is not visible because the selector is finding the element in the drop down first, and it is not visible.

Try using a more specific selector:

zone = "São Vicente"
try: 
        tiredAF = page.locator('ul#sublocations a:has-text("' + zone + '")')
        tiredAF.click()
nigh_anxiety
  • 1,428
  • 2
  • 4
  • 12