首页 > 解决方案 > 我如何使用 Selenium Python 重定向(导航)到新的 url

问题描述

我正在尝试重定向或导航到同一选项卡中当前窗口中的另一个页面,但它不起作用。
提前致谢。

url = 'https://www.nintendo.com/'
new_url = 'https://www.rockstargames.com/'
driver = webdriver.Chrome(desired_capabilities=caps, executable_path=r'/usr/local/bin/chromedriver')
driver.get(url)

# Do some stuff
getMeGames()

# This is not working 
driver.get(new_url)

# Do more Stuff
getMeVideos()

标签: pythonpython-3.xseleniumautomationselenium-chromedriver

解决方案


请确保您在“desired_capabilities=caps”中定义了“caps”变量,如果您在使用 close() 方法后没有关闭驱动程序,它仍然在内存中运行并在您尝试时会导致一些冲突再次运行脚本。

您可以在下面看到您的脚本进行了一些更改:

from selenium.webdriver import Chrome 
from time import sleep

driver = Chrome(executable_path=r'/usr/local/bin/chromedriver')

url = 'https://www.nintendo.com/'
new_url = 'https://www.rockstargames.com/'

driver.get(url)

# Do some stuff
def getMeGames():
    pass # do nothing

# This is not working 
driver.get(new_url)

# Do more Stuff
def getMeVideos():
    pass # do nothing

# waits for 3 seconds
sleep(3)

# close the current page
driver.close()

推荐阅读