How to use Selenium to select a specific tag with a class common to other tags?

Asked

Viewed 60 times

0

I’m wanting to make Crapping the most up-to-date data of this page. Every day she makes available the data of the current day and the next day. My main interest is in the data of the next day. When inspecting calendar tags (Which is a table), it is noted that all available dates (A of the next day and those of the previous days) have the class "available". The current day tag on the other hand has (in addition to the available class) the classes "active, Today, start-date and end-date". That is, only the current day tag is unique and easy to get and to be "clicked". I just need to click on the tag for the day after the current tag. I thought of using the current day tag as a reference to catch the next tag, but in addition to not finding a way to do this, I came to the conclusion that taking the next tag would give error when the current day was the last day of the week and the td tag of interest was inside the bottom tr.

Snippet of interest of the page code:

<tr>
    <td class="weekend available" data-title="r1c0">7</td>
    <td class="available" data-title="r1c1">8</td>
    <td class="available" data-title="r1c2">9</td>
    <td class="available" data-title="r1c3">10</td>
    <td class="available" data-title="r1c4">11</td>
    <td class="available" data-title="r1c5">12</td>
    <td class="weekend available" data-title="r1c6">13</td>
</tr>
<tr>
    <td class="today weekend active start-date active end-date available" data- 
    title="r2c0">14</td>
    <td class="available" data-title="r2c1">15</td>
    <td class="off disabled" data-title="r2c2">16</td>
    <td class="off disabled" data-title="r2c3">17</td>
    <td class="off disabled" data-title="r2c4">18</td>
    <td class="off disabled" data-title="r2c5">19</td>
    <td class="weekend off disabled" data-title="r2c6">20</td>
</tr>

This is the python code I’m building:

from selenium import webdriver
from time import  sleep
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options

option = Options()
option.headless = True
url = 'https://www.ccee.org.br/portal/faces/pages_publico/o-que-fazemos/como_ccee_atua/precos/preco_horario'
driver = webdriver.Chrome(ChromeDriverManager().install(),options=option)
driver.get(url)
sleep(8)
#click para abrir o calendário
driver.find_element_by_xpath("//div[@class='datefilterHorarioGrafico']//input[@id='datefilterHorarioGrafico']").click()
sleep(2)
#click para selecionar o dia
driver.find_element_by_xpath("//tbody//tr//td[@class='available']").click()

I hope you can help me. Thanks in advance.

1 answer

0


As I managed to solve alone, I will post here.

There is a Selenium parameter for you to filter by the content contained in the tag. The code is:

driver.find_element_by_xpath("//tbody//tr//td[@class='available' and contains(text(), 'CONTEÚDO')]]").click()

In this case, as the interest is to get the next day, assuming the Scrapping is done on the 14th, the code gets:

driver.find_element_by_xpath("//tbody//tr//td[@class='available' and contains(text(), '15')]]").click()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.