Is there a way to get the url of popup js onclick dialogue using selenium?


This is the website link which I am trying to scrape for data

There are links in 4th column in above site if clicked a popup window comes which has certain info along with href for the next link when we click More Information tab.We get to such links

From selenium import webdriver

    driver = webdriver.Firefox()


b = driver.find_element("xpath", '//*[@id="tollList"]/table/tbody/tr[2]/td[4]/a')

c = driver.execute_script("arguments[0].click();", b)

At this point I am stuck up as am unable to capture the href or url of the popup window… Kindly help me to get past to the other page from the pop up window

Asked By: Abhi



Those pop-ups are the result of POST requests, where the payload is each location ID. Here is a way to get the locations IDs:

from selenium import webdriver
from import Service
from import Options
from import By
from import WebDriverWait
from import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
import time as t

chrome_options = Options()

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
driver = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(driver, 25)
url = ''
places = wait.until(EC.presence_of_all_elements_located((By.XPATH, '//div[@id="tollList"]//tbody/tr/td[4]' )))
for p in places:
    p_id = p.find_element(By.XPATH, './/a').get_attribute('onclick').split('(')[1].split(')')[0]
    print(p.text, p_id)

Result in terminal:

Aganampudi 236
Amakathadu 258
Badava 4486
Bandapalli 5697
Bandlapalli 5952
Basapuram 4542
Bathalapalli 5753
Bolapalli 252

Once you have the IDs, you can go to each place’ page with{place_id}.

Answered By: Barry the Platipus