我知道如何使它单击链接,但是一旦到达新页面(新URL),就无法使其在同一会话中继续执行脚本。
当我指定新的URL时,它将打开一个新窗口,并且无法继续,因为需要从上一页插入的信息才能继续。
该如何解决?
PS。我在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")
以上代码应该可以正常运行。
答案 0 :(得分:1)
about_you_page = webdriver.Chrome()
创建新的webdriver
实例,它无权访问driver
,反之亦然。只需使用现有的driver
进行导航
driver.get("https://forsikringsguiden.dk/#!/bilforsikring/omdig")