簡體   English   中英

Python Selenium Chrome 循環瀏覽選項卡中的鏈接

[英]Python Selenium Chrome loop through links in tabs

嗨,我有一個主要的 web 頁面,該頁面在主選項卡中打開以接受 cookies 和 rest 的鏈接,這些鏈接應在選項卡中循環打開和關閉:

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])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM