javascriptpythonselenium-webdriverweb-scrapingonclick

Scrape the return of a JS onclick function


First question asked here.

I need to scrape the url generated by a JS onclick function of a website. So, I think Selenium could do the job, but I can't figure out how to code that without opening other tabs/windows and don't even know if it's possible.

Here's the onclick JS function:

'tr class="linhares" onclick="javascript:grv(0,1,1);">

So a basically need the url returned by this function.

Hope you can help me, thanks!


Solution

  • See an example about switching to new window and switch back below:

    driver.get("your-application-URL")
    time.sleep(3)
    window_before = driver.window_handles[0]
    windowHandlesAllBefore = driver.window_handles
    linkElements = driver.find_elements_by_css_selector("[onclick='javascript:grv(']")
    
    for aLink in linkElements:
        aLink.click()
        time.sleep(3)
        newWindowHandle = list(set(driver.window_handles) - set(windowHandlesAllBefore))[0]
        print(newWindowHandle)
        driver.switch_to.window(newWindowHandle)
        print(driver.current_url)
        driver.close()
        driver.switch_to.window(window_before)