0

I'm trying to build a program for automating web database managing. I'm stuck trying to affect a table in which I want to sort the items by one of the columns - in the gui it's by clicking the column on the top row: shown here on the left the default sorting and on the right the "button" which i'm trying to press

Here is the code of the first row:

<tr role="row">
    <th class="text-center sorting_asc" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 67px;" aria-sort="ascending" aria-label=": activate to sort column descending"></th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 176px;" aria-label="Vorname: activate to sort column ascending">Vorname</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 176px;" aria-label="Nachname: activate to sort column ascending">Nachname</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 176px;" aria-label="E-Mail: activate to sort column ascending">E-Mail</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 49px;" aria-label="Land: activate to sort column ascending">Land</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 176px;" aria-label="Wiedervorlage: activate to sort column ascending">Wiedervorlage</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 391px;" aria-label="Bewerbungsstatus: activate to sort column ascending">Bewerbungsstatus</th>
    <th class="sorting_disabled" rowspan="1" colspan="1" style="width: 62px;" aria-label="Aktionen">Aktionen</th>
</tr>
I found out that the classes change after pressing the key, as shown(focus on the 1st and 6th th):

<tr role="row">
    <th class="text-center sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 68.0017px;" aria-label=": activate to sort column ascending"></th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 182.002px;" aria-label="Vorname: activate to sort column ascending">Vorname</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 182.002px;" aria-label="Nachname: activate to sort column ascending">Nachname</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 182.002px;" aria-label="E-Mail: activate to sort column ascending">E-Mail</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 51.0017px;" aria-label="Land: activate to sort column ascending">Land</th>
    <th class="sorting_asc" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 182.002px;" aria-label="Wiedervorlage: activate to sort column descending" aria-sort="ascending">Wiedervorlage</th>
    <th class="sorting" tabindex="0" aria-controls="sg-datatables-applicant_datatable" rowspan="1" colspan="1" style="width: 393.002px;" aria-label="Bewerbungsstatus: activate to sort column ascending">Bewerbungsstatus</th>
    <th class="sorting_disabled" rowspan="1" colspan="1" style="width: 62px;" aria-label="Aktionen">Aktionen</th>
</tr>

Do you have any idea? I tried to click() it but it doesn't have any link and it can't function like a button, so how do I still press it?

Nimantha
  • 6,405
  • 6
  • 28
  • 69

1 Answers1

0

I struggled with this as well. Here's the solution in Python Selenium that I came up with:

driver.get(url)
script = '''document.querySelector("[aria-label='Vorname: activate to sort column ascending']").click();'''
driver.execute_script(script)

The first line of the code obtains the url with the WebDriver named driver.

The second line contains some JavaScript that clicks on the column header that has the aria label Vorname: activate to sort column ascending. It has three levels of quotation, so the top level has three ticks, the second level has two, and the lowest level has one.

The third line executes the script using the driver.

The above code sorts the table by ascending order of the column. To sort the table by descending order of the column, run the above and then run the following code:

script = '''document.querySelector("[aria-label='Vorname: activate to sort column descending']").click();'''
driver.execute_script(script)

Reference

Life is Good
  • 106
  • 9