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?

6
  • It would be helpful if u provide the url of the website. Does it require a login or something? Commented Oct 11, 2020 at 8:32
  • 1
    Yes it does unfortunately :( if you need more snippets let me know :) Commented Oct 11, 2020 at 8:34
  • And then, u have circled two elements in the screenshot. Which one of them do u wanna click? Commented Oct 11, 2020 at 8:36
  • 1
    the left one is the default sort when I open the page, the right one is the one I want to click Commented Oct 11, 2020 at 8:40
  • What have you tried so far? Commented Oct 11, 2020 at 9:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.