steve
steve

Reputation: 109

Following links across multiple pages continued

I continue to have problems navigating using NODRIVER across multiple pages. I am able to access the first page of a website hover over a drop down (using mouse.move, not hover as advised in a previous version of this question), select one of the dropdown items and advance to the second page. I clicked on the result of finding the dropdown item (pay_per_visit) and thought the details needed for the next navigation move would be stored in test1. Here is all of my code:

import asyncio
import datetime
import nodriver as uc
import tracemalloc
from time import sleep
url1 = 'https://www.mytpi.com/'

async def b_start_chrome_and_log_in():
    print("start chrome")

    driver = await uc.start()
    # Expand screen size to maximum
    #pyautogui.click(x=1181, y=38)
    await driver.main_tab.maximize()

    page = await driver.get(url1)
    print("Website loaded")

    improve = await page.find('/html/body/nav/div/ul/li[4]/a')
    print("found drop down")

    sleep(10)
    #await improve.hover()  # Hover over the dropdown
    #await improve.focus()  # Hover over the dropdown
    await improve.mouse_move()  # Hover over the dropdown
    print("hover worked")
    sleep(1)

    pay_per_visit = await page.find('/html/body/nav/div/ul/li[4]/div/div/div[2]/div/a')
    test1 = await pay_per_visit.click()

    #await driver.wait_for_navigation()
    print("Navigated to new page")

    swing = await test1.find('/html/body/div[2]/div/div/ul/li[1]/div/a/span')
    print("Found swing characteristics:", swing)

    await driver.close()


asyncio.run(b_start_chrome_and_log_in())

However, when I try to execute the next find using the test1 result, I get the following error:

> Traceback (most recent call last):
  File "C:\MyStuff\Travels\Python312\Projects\Automated Tee Times\NoDriver1FIXED TEST.py", line 42, in <module>
    asyncio.run(b_start_chrome_and_log_in())
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\MyStuff\Travels\Python312\Projects\Automated Tee Times\NoDriver1FIXED TEST.py", line 36, in b_start_chrome_and_log_in
    swing = await test1.find('/html/body/div[2]/div/div/ul/li[1]/div/a/span')
                  ^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'find'
successfully removed temp profile C:\Users\pinev\AppData\Local\Temp\uc_w5ogow8j
Exception ignored in: <function BaseSubprocessTransport.__del__ at 0x000001E43DAAB600>
Traceback (most recent call last):
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\base_subprocess.py", line 126, in __del__
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\base_subprocess.py", line 104, in close
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\proactor_events.py", line 109, in close
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\base_events.py", line 772, in call_soon
  File "C:\MyStuff\Travels\Python312\Lib\asyncio\base_events.py", line 519, in _check_closed
RuntimeError: Event loop is closedBlockquote

A previous reply (from Jonrsharpe - thanks) tried to get me going and helped me understand what the 'NoneType 'means. Despite his best intentions, I am still struggling to provide the right details to allow the last line in the following extract to work. What do I need to provide instead of test1 in the last line of the code shown below.

Thanks in advance

pay_per_visit = await page.find('/html/body/nav/div/ul/li[4]/div/div/div[2]/div/a')
test1 = await pay_per_visit.click()

#await driver.wait_for_navigation()
print("Navigated to new page")

swing = await test1.find('/html/body/div[2]/div/div/ul/li[1]/div/a/span')

Upvotes: 0

Views: 14

Answers (1)

steve
steve

Reputation: 109

The following few statements seem to resolve the issue, where the variable page carries the previous reference. After loading the new page (click in this case):

current_url = await page.evaluate("window.location.href")  # Get the new url
page = await driver.get(current_url)

Upvotes: 0

Related Questions