简体   繁体   中英

Python Selenium Chromedriver - Can't Get current_url of new opened tab after click()

The current code takes me to the correct url, but ends up just printing the url I was at before.

for i in range(25):
        driver.find_element(By.XPATH, f'//*[@id="listing_{i}"]').click()
        print(driver.current_url)

I want to get the new current_url after performing the click() request

You can look into this question here for more help as your question is the same. The problem here is that you have switched to a new tab and the WebDriver no longer has context. It thinks you're still in the first tab. In order to fix this you need to switch your WebDriver to the most recently opened tab using this call: (Put this after the click() and before the print())

driver.switch_to.window(driver.window_handles[-1]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM