Python and Selenium: can't switch to current_url after hitting click

Viewed 28

I have the following code that places a string in the search field and then clicks (website is Google Play Store):

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("https://play.google.com/store")
driver.find_element_by_css_selector('.VfPpkd-Bz112c-LgbsSe.yHy1rc.eT1oJ.mN1ivc').click()
inputElement = driver.find_element_by_css_selector(".HWAcU")
inputElement.send_keys('HaloVPN: Fast Secure VPN Proxy')
inputElement.send_keys(Keys.ENTER)
driver_two = webdriver.Firefox()
driver_two.get(driver.current_url)
elems = driver_two.find_elements_by_css_selector(".Si6A0c.Gy4nib")
links = [elem.get_attribute('href') for elem in elems]

This code works fine, but I have to pop up two browsers for it (this is my doing, not the behavior of the website). I don't want to have to open two browsers, since the Play Store actually refreshes the contents after hitting click.

I tried doing this, but after trying to scrape it, I realized I'm still scraping my initial URL, and not the second one generated after the search.

driver.switch_to.window(driver.window_handles[0])

It is indexed to zero because length is 1, which would be the case if I don't want to open a second browser. I can tell the driver.current_url parameter gives me the correct URL, but, when I go to scrape it, the contents belong to the initial URL and not the secondary one.

How can I switch to my secondary URL without having to open a secondary browser?

Thanks in advance!

0 Answers
Related