0

Python 3.10.5
Selenium 4.3.0
Seleniumwire 4.6.5
geckodriver 0.31.0
Firefox 91.11.0esr (64-bit)
IDE VSCode 1.70.1

I'm getting a very strange behaviour while running my code in a python runtime and at the same time while running it in a debug mode in VSCode built-in debugger it works as expected. Also, I need to get image link and paragraph text from preview on the wikipedia website, which you see when you hover mouse on the hyperlink.

Here it is:

def get_breed(a, driver, action):

    url = unquote(a.get_attribute("href"))
    if page_exist(url):
        scroll_shim(driver, a)
        action.move_to_element(a).perform()
        div_popup = driver.find_element(By.CLASS_NAME, "mwe-popups-container")
        if div_popup:
            try:
                img = div_popup.find_element(By.CLASS_NAME, "mwe-popups-thumbnail")
                p = div_popup.find_element(By.TAG_NAME, "p")
                print("test-1")
            except NoSuchElementException:
                print("test-4")
                print(err)
                return

            print("test-2")
            breed = {
                "name": a.text,
                "img": unquote(img.get_attribute("src")),
                "desc": p.text
                }
            return breed

In my current case there is a preview without an image and I want to handle with such cases like in a code with try/exec above and with if in code below (if False return doing a continue in for loop):

def get_breeds_from_ul(breeds_url):

    driver = get_driver(breeds_url)
    action = ActionChains(driver)
    uls = driver.find_elements(By.XPATH, "//div[@class='mw-parser-output']/ul")

    breeds = []
    for ul_tag in uls[:-2]:
        all_a = ul_tag.find_elements(By.TAG_NAME, "a")
        for a in all_a:
            breed = get_breed(a, driver, action)
            if breed:
                breeds.append(breed)
                print(breed)
                print("test-3")
    driver.close()
    return breeds

But instead of catching "NoSuchElementException" when it obviously comes it seems that python ignores exception, goes further to "return breed" block and after returning it, appends it to list breeds (instead of going to if block in "get_breeds_from_ul" and just continuing in loop further). If add some debug prints (print("test-x")), I get, that code returns to except block after appending to list in the parent function (and print "test-4" with err variable from there), that blows my mind... What am I doing or understanding wrong?

Here is a configuration of my VSCode debugger:

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "justMyCode": true
        }
    ]
}

Here is a result:

$ python get_animals.py
test-1
test-2
{'name': 'Bayerische Landgans', 'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Bayerische_Landgans.jpg/319px-Bayerische_Landgans.jpg', 'desc': 'Bayerische Landgänse sind Hausgänse, die in Franken, der Oberpfalz, Niederbayern und Schwaben gezüchtet wurden. „Bayerische Landgans“ ist ein Oberbegriff für die in diesen Regionen beheimateten Landschläge.'}
test-3
test-1
test-2
{'name': 'Rassestandard', 'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Bayerische_Landgans.jpg/319px-Bayerische_Landgans.jpg', 'desc': 'Bayerische Landgänse sind Hausgänse, die in Franken, der Oberpfalz, Niederbayern und Schwaben gezüchtet wurden. „Bayerische Landgans“ ist ein Oberbegriff für die in diesen Regionen beheimateten Landschläge.'}
test-3
test-4
Message: Unable to locate element: .mwe-popups-thumbnail
Stacktrace:
WebDriverError@chrome://remote/content/shared/webdriver/Errors.jsm:181:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.jsm:393:5
element.find/</<@chrome://remote/content/marionette/element.js:305:16

PS: the last message isn't a raised exception. It's just a printed value of "err" variable.

UPDATE: I expect that 'Rassestandard' dictionary won't be printed because it's an "a" tag without image (and not a page about an animal). I expect the next one:

$ python get_animals.py
test-1
test-2
{'name': 'Bayerische Landgans', 'img': 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/8b/Bayerische_Landgans.jpg/319px-Bayerische_Landgans.jpg', 'desc': 'Bayerische Landgänse sind Hausgänse, die in Franken, der Oberpfalz, Niederbayern und Schwaben gezüchtet wurden. „Bayerische Landgans“ ist ein Oberbegriff für die in diesen Regionen beheimateten Landschläge.'}
test-3
test-1
test-4
test-1
test-2
{'name': 'Another animal name', 'img': 'another link', 'desc': 'Another description.'}
test-3
Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55

0 Answers0