doomdaam
doomdaam

Reputation: 783

Selenium click link and continue on new URL - cannot continue script on new site

I know how to make it click a link, but once I get to the new page (new URL) I cannot make it continue the script in the same session.

It opens a new window when I specify the new URL, and cannot continue as the inserted info from previous page is necessary to continue.

How to fix this?

PS. I work in python

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.keys import Keys
import time

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")

# enable browser logging
d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' } 
driver = webdriver.Chrome(desired_capabilities = d, options=chrome_options)
driver.fullscreen_window()

#URL
driver.get("https://forsikringsguiden.dk/#!/bilforsikring/manuel")

#remove cookie bar
driver.find_element_by_id('cookieBarAccept').click()        

#car name
maerke = driver.find_element_by_xpath('//*[@id="s2id_carSelectedMake"]/a').click()
driver.find_element_by_xpath('//*[@id="s2id_autogen1_search"]').send_keys("Hyundai")
driver.minimize_window()
driver.maximize_window()
driver.find_element_by_xpath('//*[@id="select2-drop"]').click()

#model
model = driver.find_element_by_xpath('//*[@id="s2id_autogen2"]').click()
driver.find_element_by_xpath('//*[@id="s2id_autogen3_search"]').send_keys("i30")
driver.minimize_window()
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="select2-drop"]').click()

#scrolling down
driver.execute_script("scrollBy(0,250)")         

#year
aargang = driver.find_element_by_xpath('//*[@id="s2id_autogen4"]/a').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="s2id_autogen5_search"]').send_keys("2009")
driver.minimize_window()
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="select2-drop"]').click()

#engine size
driver.execute_script("scrollBy(0,250)")         
motor_str = driver.find_element_by_xpath('//*[@id="s2id_autogen6"]/a').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="s2id_autogen7_search"]').send_keys("1,6")
driver.minimize_window()
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="select2-drop"]').click()

#engine variant 
variant = driver.find_element_by_xpath('//*[@id="s2id_autogen8"]').click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="s2id_autogen9_search"]').send_keys("1,6 CRDi 116HK 5d")
driver.minimize_window()
driver.maximize_window()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="select2-drop"]').click()

#scrolling down
driver.execute_script("scrollBy(0,250)")         

#clicks on link
godkend_oplysninger = driver.find_element_by_xpath('//*[@id="content"]/div[4]/form/div[6]/div/button').click()

#Continue on new URL
about_you_page = webdriver.Chrome(desired_capabilities = d, options=chrome_options)
about_you_page.get("https://forsikringsguiden.dk/#!/bilforsikring/omdig")

#age
#Fills out first field - "Din Alder"
about_you_page.find_element_by_xpath('//*[@id="content"]/div/div[2]/div[2]/form/div[1]/div[1]/div/input').click().send_keys("50")

Above code should function without any issues.

Upvotes: 0

Views: 427

Answers (1)

Guy
Guy

Reputation: 50899

about_you_page = webdriver.Chrome() creates new webdriver instance, it doesn't have any access to the driver and vice versa. Just use the existing driver to navigate

driver.get("https://forsikringsguiden.dk/#!/bilforsikring/omdig")

Upvotes: 1

Related Questions