Python Selenium Chrome 遍历选项卡中的链接
Python Selenium Chrome loop through links in tabs
您好,我有一个在主选项卡中打开以接受 cookie 的主网页,其余链接应在选项卡中循环打开和关闭:
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
但这不起作用,有什么解决方法的建议吗?
谢谢。
这应该有效:
您基本上到达了 window_handle[1],但没有切换回父 window,因此出现了问题
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
driver.switch_to.window(driver.window_handles[0])
您好,我有一个在主选项卡中打开以接受 cookie 的主网页,其余链接应在选项卡中循环打开和关闭:
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
但这不起作用,有什么解决方法的建议吗?
谢谢。
这应该有效:
您基本上到达了 window_handle[1],但没有切换回父 window,因此出现了问题
links = ['https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/','https://www.deviceinfo.me/']
driver = webdriver.Chrome(executable_path=r'C:\ProgramFiles(x86)\chromedriver.exe')
driver.get('https://www.deviceinfo.me/') #open the main tab
for link in links:
driver.execute_script("window.open();") # open a new tab
driver.switch_to.window(driver.window_handles[1]) # switch to the tab 1
driver.get(link)
driver.close() # close tab 1
driver.switch_to.window(driver.window_handles[0])