1

I'm scraping a web page whose elements change if the user downloads a file with many transactions

for example, if the web is "clean", I access an element with:

start_date = driver.find_element_by_id('zk_comp_172-real')

but if the user manually requests many transactions a link to the file is set at the top of the page for 48 hours and all the HTML's elements are "displaced":

enter image description here

So I handle this by a try-catch approach:

try:
    ...
    start_date = driver.find_element_by_id('zk_comp_172-real')
    ...

except:
    ....
    start_date = driver.find_element_by_id('zk_comp_181-real')
    .....

But turns out that this id changes based on the number of files request that the user has done. So I'm trying to catch two exceptions, the first one to catch this error: ...

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="zk_comp_172-real"]"}

and the second one to catch the previous one:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="zk_comp_181-real"]"}

the last exception would identify the element I want if the user requests two files:

 start_date = driver.find_element_by_id('zk_comp_178-real')

But I don't get how to achieve this with multiple except statements, I've tried the next:

try:
    ...
    start_date = driver.find_element_by_id('zk_comp_172-real')
    ...

except selenium.common.exceptions.NoSuchElementException:
    ....
    start_date = driver.find_element_by_id('zk_comp_181-real')
    .....
except Exception:
    ....
    start_date = driver.find_element_by_id('zk_comp_178-real')
    .....

But I receive the traceback with the two exceptions but it seems the last except is not executed:

Traceback (most recent call last)

During handling of the above exception, another exception occurred:

Traceback (most recent call last): ...

The two exceptions are of the same type, so I don't know how to set the last exception to work properly, any suggestion with that?

1 Answer 1

2

If I understand the requirements correctly, you need to stack the exceptions:

try:
    ...
    start_date = driver.find_element_by_id('zk_comp_172-real')
    ...

except selenium.common.exceptions.NoSuchElementException:
    try:
       ....
       start_date = driver.find_element_by_id('zk_comp_181-real')
       .....
    except selenium.common.exceptions.NoSuchElementException:
       ....
       start_date = driver.find_element_by_id('zk_comp_178-real')
       .....

You can also add the IDs to a list and loop until the element is found.

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.