Python Forum
Try and Exception not properly working with xpath
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Try and Exception not properly working with xpath
#1
This is a small code segment of a program and below the output is given -
    try:
        print('li 8')
        lim = driver.find_element_by_xpath('//*[@id="names_load_paginate"]/ul/li[8]/a').text
    except:
        lim = 6
        while lim >= 2:
            print(lim)
            list_string = '//*[@id="names_load_paginate"]/ul/li[' + str(lim) + ']/a'
            try:
                print(lim)
                lim = driver.find_element_by_xpath(list_string).text
                print(lim)
                break
            except:
                print("ex lim")
                lim = lim - 1
For this code segment I am getting the below output -
Output:
li 8 6 6 ex lim 5 5 ex lim 4 4 ex lim 3 3
I don't understand why the code is stopped executing at 'lim = 3', though it should raise an exception for 'lim = 3' and continue then to stop at "lim = 2" because XPATH - "//*[@id="names_load_paginate"]/ul/li[2]/a" is present only in the web page no any other list element are present there.
And if the following line
lim = driver.find_element_by_xpath(list_string).text
executed for lim = 3 then three values should be printed, because there is a print statement after the above mentioned line which is missing in output. Please share your views.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  File Handling not working properly TheLummen 8 740 Feb-17-2024, 07:47 PM
Last Post: TheLummen
  fpdf orientation not working properly KatMac 1 3,339 May-02-2021, 10:47 AM
Last Post: Pedroski55
Lightbulb Jupyter is not working properly brunolelli 3 2,987 Apr-23-2021, 03:22 AM
Last Post: Larz60+
  file.write not working properly mnh001 11 4,481 Nov-09-2019, 10:20 PM
Last Post: mnh001
  hatching not working properly with matplotlib Staph 3 3,031 Jul-28-2019, 07:17 AM
Last Post: ThomasL
  During handling of the above exception, another exception occurred Skaperen 7 26,892 Dec-21-2018, 10:58 AM
Last Post: Gribouillis
  import not working properly mepyyeti 5 4,378 Jan-18-2018, 07:08 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020